[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Transform flat structure into hierarchy

Subject: Re: Transform flat structure into hierarchy
From: Peter Davis <pdavis152@xxxxxxxxx>
Date: Mon, 11 Mar 2002 15:47:45 -0800
flat structure
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.