xsl-list
[Top] [All Lists]

Re: Convert sequence of elements to nested structure

2004-12-24 07:03:28
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(_dot_)bradley(_at_)herbertgroup(_dot_)com>
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>