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

Re: Another problem from Flat XML File to Hierarchical

Subject: Re: Another problem from Flat XML File to Hierarchical
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Mon, 12 Feb 2007 17:18:50 +0100
Re:  Another problem from Flat XML File to Hierarchical
Hi Alex,

You didn't tell us what XSLT version you are using. I try with XSLT 2. Somehow I often have trouble with even rather flat to hierarchical questions. I am sure somebody else can do much better. The below solution works correctly if you add 'level1' to the top of your input xml. If you don't want that, you should adjust the patterns a bit:


<xsl:template match="/"> <xsl:for-each-group select="$data/*" group-starting-with="level1">

       <xsl:copy>
           <xsl:apply-templates select="current-group()[2]" />
       </xsl:copy>
   </xsl:for-each-group>
</xsl:template>

<xsl:template match="*[matches(name(), 'level')]">
   <xsl:variable name="currentname" select="name()" />
   <xsl:for-each-group
       select="current-group()[position() > 1]"
       group-starting-with="*[name() = $currentname]">

       <xsl:copy>
           <xsl:apply-templates select="current-group()[2]" />
       </xsl:copy>
   </xsl:for-each-group>
</xsl:template>


It's a variant to the tree-traversal technique, as it always selects one higher than the current node.


Hope this helps,

Cheers,
-- Abel Braaksma
  http://xslt.metacarpus.com


alex v. wrote:
I have a flat xml like this:

<level2></level2>
<level3></level3>
<level2></level2>
<level1></level1>
<level2></level2>
<level3></level3>
<level3></level3>

and I need to transform in a hierarchy structure like this using the
tag name as key:

<level2>
<level3></level3>
</level2>
<level2></level2>
<level1>
<level2>
<level3></level3>
<level3></level3>
</level2>
</level1>


Thanks in advance.


~av

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-2011 All Rights Reserved.