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

Re: Convert sequence of elements to nested structure

Subject: Re: Convert sequence of elements to nested structure
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Fri, 24 Dec 2004 06:03:28 -0800 (PST)
nested structure
Hi Rowan,
  Please try this XSL -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
<xsl:output method="xml" indent="yes" />
  
<xsl:template match="/root">
  <root>
    <xsl:for-each select="*">
      <xsl:choose>
        <xsl:when test="name(.) != 'note'">
          <xsl:copy-of select="." />
        </xsl:when>
        <xsl:when test="name(preceding-sibling::*[1])
!= 'note'">
          <note>
            <xsl:value-of select="." />
            <xsl:call-template name="create-group">
              <xsl:with-param name="node-set"
select="following-sibling::*" /> 
            </xsl:call-template>
          </note>
        </xsl:when>
      </xsl:choose>  
    </xsl:for-each>
  </root>
</xsl:template>
  
<xsl:template name="create-group">
  <xsl:param name="node-set" />
    
  <xsl:choose>
    <xsl:when test="(name($node-set[1]) = 'note') and
(name($node-set[2]) != 'note')">
      <cont><xsl:value-of select="$node-set[1]"
/></cont>
    </xsl:when>
    <xsl:when test="(name($node-set[1]) = 'note') and
(name($node-set[2]) = 'note')">
      <cont><xsl:value-of select="$node-set[1]"
/></cont>
      <xsl:call-template name="create-group">
        <xsl:with-param name="node-set"
select="$node-set[position() > 1]" /> 
      </xsl:call-template>
    </xsl:when>
  </xsl:choose>  
</xsl:template>
  
</xsl:stylesheet>

When it is applied to XML -
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <a>content1</a>
  <note>content2</note>
  <note>content3</note>
  <note>content4</note>
  <b>content5</b>   
</root>

It produces o/p -
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <a>content1</a>
   <note>content2
      <cont>content3</cont>
      <cont>content4</cont>
   </note>
   <b>content5</b>
</root>

I wish all XSL-List members Merry Christmas and a
Happy New Year!

Regards,
Mukul

--- Rowan Bradley <rowan.bradley@xxxxxxxxxxxxxxxx>
wrote:

> Hi,
> 
> This is no doubt an elementary question but I need
> to convert a sequence 
> of elements of the same name like this:
> 
> <a>content1</a>
> <note>content2</note>
> <note>content3</note>
> <note>content4</note>
> <b>content5</b>
> 
> into a nested structure like this:
> 
> <a>content1</a>
> <note>content2
>      <cont>content3</cont>
>      <cont>content4</cont>
> </note>
> <b>content5</b>
> 
> In other words, if there are consecutive <note>
> elements, they should be 
> combined into one <note> element with the 2nd, 3rd
> and subsequent lines 
> expressed as <cont> (continuation line) elements
> within the <note> element.
> 
> I can't quite see how to do this by recursion.
> 
> Thanks for your help
> 
> Rowan




		
__________________________________ 
Do you Yahoo!? 
Dress up your holiday email, Hollywood style. Learn more. 
http://celebrity.mail.yahoo.com

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.