This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: file manipulation with recursion


--- Holmberg Rick-ra0119 <Rick dot Holmberg at motorola dot com> wrote:

 
> Hi all,
>    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>


Hi Rick,

The following transformation produces the desired results:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 >
 
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="category/*[not(self::id 
                                   or self::name
                                   or self::category
                                      )
                                  ]"/>
</xsl:stylesheet>


This is just the identity rule plus a rule to eliminate any children of
a "category", which are not "id" or "name" or "category" themselves.

And when applied to your source xml document, the result is exactly as
desired:

<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>

Hope this helped.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]