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

Re: From flat to hierarchical structure

Subject: Re: From flat to hierarchical structure
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 22 Oct 2014 11:34:04 -0000
Re:  From flat to hierarchical structure
nick public nickpubl@xxxxxxxxx wrote:

I need to convert a flat structure like this

<root>
    <H>1</H>
    <I>1-1</I>
    <I>1-2</I>
    <I>1-3</I>
    <H>2</H>
    <I>2-1</I>
    <I>2-2</I>
  </root>

in one like this

<root>
    <H>
       1
       <I>1-1</I>
       <I>1-2</I>
       <I>1-3</I>
    </H>
    <H>
       2
       <I>2-1</I>
       <I>2-2</I>
    </H>
</root>

Can't you use XSLT 2.0 with


  <xsl:template match="root">
    <xsl:copy>
      <xsl:for-each-group select="*" group-starting-with="H">
        <xsl:copy>
          <xsl:apply-templates select="node(), current-group() except ."/>

? That way it is easy to process your input.

With XSLT 1.0 you might want to define a key

<xsl:key name="group" match="I" use="generate-id(preceding-sibling::H[1])"/>

and then you can use

<xsl:template match="root">
  <xsl:copy>
    <xsl:apply-templates select="H"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="H">
  <xsl:copy>
    <xsl:apply-templates select="node() | key('group', generate-id())"/>
  </xsl:copy>
</xsl:template>

Both approaches need the identity transformation template to copy the remaining nodes like the I elements.

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.