Subject: Re: [xsl] Convert sequence of elements to nested structure From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx> Date: Fri, 24 Dec 2004 06:03:28 -0800 (PST) |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Convert sequence of elements , Rowan Bradley | Thread | [xsl] string manipulation language , Bryan Rasmussen |
[xsl] string manipulation language , Bryan Rasmussen | Date | [xsl] XSL Transformation!!, Kalkunda, Venkat |
Month |