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: Re: file manipulation with recursion


Thanks for all your help.  I have a little twist to this file however.  Under each category node there could be zero, one or many product nodes.  I want to just display the id and name of the product node as well, and they can appear under any category node.  The initial file format would be of the following...


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>
						<product>
							<id>abc123</id>
                             			<type>new</type>
                             			<name>myProduct</name>
                             			<releaseLevel>Live</releaseLevel>
                             			<date>2002-07-11 14:52:06</date>
						</product>
						<product>
							<id>abc987</id>
                             			<type>new</type>
                             			<name>myProduct3</name>
                             			<releaseLevel>Live</releaseLevel>
                             			<date>2002-07-11 14:52:06</date>
						</product>

                       </category>
					<product>
						<id>asd123</id>
                             		<type>old</type>
                             		<name>myProduct2</name>
                             		<releaseLevel>Live</releaseLevel>
                             		<date>2002-07-11 14:52:06</date>
					</product>
            </category>
		<category>
                 <id>13abc</id>
                 <level>1</level>
                 <name>Cat2</name>
                 <releaseLevel>Live</releaseLevel>
                 <date>2002-01-07 14:02:41</date>
		</category>
     </category>
 </hierarchy>


Thanks again for your help!

Rick

-----Original Message-----
From: Dimitre Novatchev [mailto:dnovatchev@yahoo.com]
Sent: Tuesday, July 23, 2002 8:41 AM
To: xsl-list@lists.mulberrytech.com
Subject: [xsl] 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

 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]