|
Subject: Re: [xsl] file manipulation with recursion From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Tue, 23 Jul 2002 14:49:30 +0100 |
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
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| [xsl] file manipulation with recurs, Holmberg Rick-ra0119 | Thread | [xsl] Documenting xsl code II, "Braumüller, Hans" |
| RE: [xsl] encoding woes: ISO-8859-1, Xiaocun Xu | Date | [xsl] Dynamic XSL Sorting - Is ther, Robert . Franklin |
| Month |