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

RE: Re: file manipulation with recursion

Subject: RE: Re: file manipulation with recursion
From: Holmberg Rick-ra0119 <Rick.Holmberg@xxxxxxxxxxxx>
Date: Tue, 23 Jul 2002 07:16:22 -0700
date manipulation in files
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@xxxxxxxxx]
Sent: Tuesday, July 23, 2002 8:41 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  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


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.