xsl-list
[Top] [All Lists]

Re: Process same template 2X?

2002-10-21 06:22:58
OK I figured out my own question (hurray). I needed to use a parameter:
<xsl:template match="category">
   <xsl:param name="lastCatName"><xsl:value-of select="@name"/></xsl:param>
   <xsl:if test="@level=1">
   <category>

       <xsl:attribute name="name">
           <xsl:value-of select="@name"/>
       </xsl:attribute>
<xsl:for-each select="following-sibling::category">
   <xsl:if test="@parent=$lastCatName">
       <subcat>
           <xsl:attribute name="name">
           <xsl:value-of select="@name"/>
           </xsl:attribute>
       </subcat>
   </xsl:if>
   </xsl:for-each>
    </category>


Geoff Hankerson wrote:

Thanks this almost does it but instead of the if statement testing "level=2" I really need to test the name parameter of the current category node to see if it is the same as the parent parameter any of the following category nodes (see original post at bottom) and this has me scratching my head... Any suggestions??

Nirmala R wrote:

Hi,

I assume that you want to put all the category with level2 under level1 as
subcat.

You can have a stylesheet like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output indent="yes" method="xml"/>
       <xsl:template match="/cats">
       <cats>

           <xsl:apply-templates select="category"/>

       </cats>
   </xsl:template>

   <xsl:template match="category">
   <xsl:if test="@level=1">
   <category>

       <xsl:attribute name="name">
           <xsl:value-of select="@name"/>
       </xsl:attribute>

    <xsl:for-each select="following::category">
    <xsl:if test="@level=2">
       <subcat>
           <xsl:attribute name="name">
           <xsl:value-of select="@name"/>
           </xsl:attribute>
       </subcat>
    </xsl:if>
    </xsl:for-each>
   </category>
   </xsl:if>
   </xsl:template>
   </xsl:stylesheet>

This will take care of the case excluding a sibling with the category level
as 1 also inaddition to your requirement.

Hope I answered your query,
Nirmala

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com]On Behalf Of 
Geoff
Hankerson
Sent: Saturday, October 19, 2002 1:22 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Process same template 2X?

I want to transform this xml :

<?xml version="1.0" encoding="UTF-8"?>
<cats>
<category level="1" name="Business Law"/>
<category level="2" name="Corporations" parent="Business Law">
</category>
<category level="2" name="Nonprofit Corporations*" parent="Business Law">
</category>
</cats>

Into this xml:
<?xml version="1.0" encoding="UTF-8"?>
<cats>
<category name="Business Law"/>
   <subcat name="Corporations"/>
   <subcat name="Nonprofit Corporations*">
</category>
</cats>

It seems it should be simple but is giving me fits. Current stylesheet
(stinks) is this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output indent="yes" method="xml"/>
       <xsl:template match="/cats">
       <cats>

           <xsl:apply-templates select="category"/>

       </cats>
   </xsl:template>
   <xsl:template match="category">
   <xsl:if test="@level=1">
   <category>

       <xsl:attribute name="name">
           <xsl:value-of select="@name"/>
       </xsl:attribute>
       <subcat>
           <xsl:attribute name="name">
           <xsl:value-of select="following-sibling::category/@name"/>
           </xsl:attribute>

       </subcat>
   </category>
   </xsl:if>
   </xsl:template>
   </xsl:stylesheet>

Current stlyesheet returns this:
<?xml version="1.0" encoding="UTF-8"?>

<cats>

 <category name="Business Law">

   <subcat name="Corporations"/>

 </category>

</cats>

the 3rd category node in the orginal xml does not get processed how can
I assure all fllowing category nodes get processed?

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

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



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





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