Subject: RE: [xsl] xslt: Wrapping multiple elements in a parent element From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> Date: Fri, 09 May 2003 12:48:05 -0400 |
<?xml version="1.0"?> <Root> <a> <b> <c> <d>sundar</d> <e> <f>fff</f> </e> </c> <d>sundar2</d> </b> </a> <a> <b> <d>sundar3</d> </b> </a> <d>sundar4</d> </Root>
I am trying to group all the <d> elements separately and rest of the document will be same. <d> elements will be grouped under <D> element.
This is the stylesheet which does that:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" />
<xsl:template match="/Root"> <Root> <xsl:apply-templates select="*[name(.)!='d']"/> <D> <xsl:apply-templates select="//d"/> </D> </Root> </xsl:template>
<xsl:template match="*"> <xsl:element name="{name(.)}"> <xsl:value-of select="text()"/> <xsl:apply-templates select="*[name(.)!='d']"/> </xsl:element> </xsl:template>
<xsl:template match="d"> <d> <xsl:value-of select="text()"/> </d> </xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" />
<xsl:template match="/Root"> <Root> <xsl:apply-templates/> <D> <xsl:apply-templates mode="dMode"/> </D> </Root> </xsl:template>
<xsl:template match="*"> <!-- copies elements in default traversal --> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template>
<xsl:template match="d"/> <!-- suppresses d element in default traversal -->
<xsl:template match="d" mode="dMode"> <!-- in dMode, we copy the d --> <xsl:copy-of select="."> </xsl:template>
<xsl:template match="text()" mode="dMode"/> <!-- when in dMode, we want to suppress everything else; this accomplishes that by suppressing the text nodes -->
<xsl:template match="*"> <xsl:apply-templates/> </xsl:template>
<xsl:template match="text()"> <xsl:value-of select="."/> </xsl:template>
<xsl:template match="*" mode="dMode"> <xsl:apply-templates mode="dMode"/> </xsl:template>
Cheers (and thanks to Sundar) -- Wendell
___&&__&_&___&_&__&&&__&_&__&__&&____&&_&___&__&_&&_____&__&__&&_____&_&&_ "Thus I make my own use of the telegraph, without consulting the directors, like the sparrows, which I perceive use it extensively for a perch." -- Thoreau
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] xslt: Wrapping multiple e, David Neary | Thread | [xsl] Sorting alpha numeric values, Hugh Dixon |
RE: [xsl] Problem solution with IE5, Shenan Hawkins | Date | [xsl] Perpetuating xsl instructions, Alan Gardner |
Month |