[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Transform flat structure into hierarchy


Subject: Re: [xsl] Transform flat structure into hierarchy
From: Peter Davis <pdavis152@xxxxxxxxx>
Date: Mon, 11 Mar 2002 15:47:45 -0800

I'd do a recursive template that selects only those PublicationElements that 
are children of the current element.  I don't think there is any need to use 
the Muenchian method.

<!-- this is untested -->
<xsl:template match="Publication">
  <xsl:apply-templates select="PublicationElements[ParentIndex = -1]"/>
</xsl:template>

<!-- copy all nodes that don't have their own template -->
<xsl:template match="node() | @*">
  <xsl:copy>
    <xsl:apply-templates select="node() | @*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="PublicationElements">
  <!-- copy the <PublicationElements> and all the children, and also copy 
<PublicationElements>s that have the current node set as the parent -->
  <xsl:copy>
    <xsl:apply-templates select="node() | @*"/>
    <xsl:apply-templates
      select="../PublicationElements[ParentIndex = current()/SelfIndex]"/>
  </xsl:copy>
</xsl:template>


If you think using keys would be faster, then you could also add:
<xsl:key name="children" match="PublicationElements" use="ParentIndex"/>
and replace the apply-templates with:
<xsl:apply-templates select="key('children', -1)"/>
and
<xsl:apply-templates select="key('children', SelfIndex)"/>

Either way, it's not really a grouping problem.  It's just a problem of 
filtering out the unwanted elements at each level (those that have 
ParentIndex != current()/SelfIndex), copying the current element, and then 
repeating the process for the next level down.


On Monday 11 March 2002 07:13, Scherpenzeel, Wim wrote:
> I have an XML file (see below) that has a 'flat' structure and I need to
> transform it into an hierarchical structure (see below).
>
> I am trying my best with the Muenchian method etc, but I cannot get it
> quite right.
> Anybody out there want to give this one a try?
>
> Thanks
> Wim
>
> --------------------
> Input file:
> --------------------
> <Publication>
>   <PublicationElements>
>     <ParentIndex>-1</ParentIndex>
>     <SelfIndex>0</SelfIndex>
>     <LabelTypeName>Volgorde</LabelTypeName>
>     <LabelName>Folderflap</LabelName>
>     <ElementText>
>     </ElementText>
>   </PublicationElements>
>   <PublicationElements>
>     <ParentIndex>0</ParentIndex>
>     <SelfIndex>1</SelfIndex>
>     <LabelTypeName>Volgorde</LabelTypeName>
>     <LabelName>frontpage</LabelName>
>     <ElementText>
>     </ElementText>
>   </PublicationElements>
>   <PublicationElements>
>     <ParentIndex>1</ParentIndex>
>     <SelfIndex>2</SelfIndex>
>     <LabelTypeName>Volgorde</LabelTypeName>
>     <LabelName>headerbox</LabelName>
>     <ElementText>
>     </ElementText>
>     <Functions>Pre#F#Box</Functions>
>   </PublicationElements>

-- 
Peter Davis
Change the Social Contract?  BWAHAHAHAHAHAHAHAHAHAHAHA.
	-- Branden Robinson

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords
xml