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]

file manipulation with recursion


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>
 
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.  
 
 
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
   <xsl:output method="xml" indent="yes" /> 
 
<xsl:template match="hierarchy">
 <New_hierarchy>
  <xsl:apply-templates />
 </New_hierarchy>
   </xsl:template>
        
   <xsl:template name="category" match="category">
    <xsl:param name="index" select="'1'"/>
          <xsl:variable name="category" select="category"/>
          <xsl:variable name="relLevel" select="releaseLevel"/>
          <xsl:if test='$relLevel="Live"'>
           <category>
               <id><xsl:value-of select="category[$index]/id"/></id>
               <name><xsl:value-of select="category[$index]/name"/></name>
               <xsl:call-template name = "category">
                  <xsl:with-param name="index" select="$index+1"/>
               </xsl:call-template>      
           </category>  
          </xsl:if>
   </xsl:template>
 
   <xsl:template match="text()|@*">
   </xsl:template>
 
</xsl:stylesheet>

 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]