|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: file manipulation with recursion
Hi Rick,
> I am new to XML/XSLT and have a file that I need to generate a
> smaller version of. The file is of the format:
>
> <hierarchy>
> <category>
> <id>0</id>
> <level>-1</level>
> <name>Cat1</name>
> <releaseLevel>Live</releaseLevel>
> <date>2002-02-25 12:29:46</date>
> <category>
> <id>13abc</id>
> <level>1</level>
> <name>Cat2</name>
> <releaseLevel>Live</releaseLevel>
> <date>2002-01-07 14:02:41</date>
> <category>
> <id>X12345</id>
> <level>2</level>
> <name>Cat3</name>
> <releaseLevel>Live</releaseLevel>
> <date>2002-07-11 14:52:06</date>
> </category>
> </category>
> </category>
> </hierarchy>
>
> I need to have the output file be of the format
> <hierarchy>
> <category>
> <id>0</id>
> <name>Cat1</name>
> <category>
> <id>13abc</id>
> <name>Cat2</name>
> <category>
> <id>X12345</id>
> <name>Cat3</name>
> </category>
> </category>
> </category>
> </hierarchy>
>
> I have to use recursion because I need the close each of the
> category tags in the output file. I am not sure how to iterate
> through these nodes. I have tried several ways but have had no luck.
> I dont' think that I can use a for-each because the depth of the
> categories will change and will not be known when I am processing
> the file. This example shows the categories 3 deep but in actuality
> it will be anywhere from 4 to 10 deep. Below is the latest version
> of what I have been trying. Any help would be appreciated.
This is easier than you think. For each category you want to create a
category element, use the id and name from the category element, and
then go on and process the category elements that it contains. Try:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="hierarchy">
<xsl:copy>
<xsl:apply-templates select="category" />
</xsl:copy>
</xsl:template>
<xsl:template match="category">
<xsl:copy>
<xsl:copy-of select="id" />
<xsl:copy-of select="name" />
<xsl:apply-templates select="category" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








