[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: file manipulation with recursion

Subject: Re: file manipulation with recursion
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 23 Jul 2002 14:49:30 +0100
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.