xsl-list
[Top] [All Lists]

Re: recursive help

2003-09-26 12:45:29
It seems you can accomplish the transformation without
recursion.. Below are the templates.

<xsl:template match="/">
  <xsl:apply-templates select="//standard" />
</xsl:template>
        
<xsl:template match="//standard">
    <xsl:if test="standard">
       <h2><xsl:value-of select="@name" /></h2>
    </xsl:if>

    <xsl:if test="not(standard)">
       <a><xsl:attribute
name="href">addlinkinfol8r</xsl:attribute><xsl:value-of
select="@name" />
       </a>
    </xsl:if>
</xsl:template>

Regards,
Mukul

--- "Wagstaff, Jason" <WagstaffJ(_at_)msx(_dot_)umsl(_dot_)edu> wrote:
i have an xml document that needs to be transformed
which seems to perfect for recursion. (this is my
first attempt at trying this) it is essentialy a
nested menu system of links. the basic logic is it
checks to see if a node has a child, if it does
print a header otherwise a link.    i built the xml
document using recursion, i just can't figure out
how to transform it useing the same technique.
samples of both xml and xslt 


xml doc:
<?xml ?>
<standards>
   <standard id="1" name="test">
        <standard id ="32" name="test" />
        <standard id ="45" name="test>
             <standard id ="23" name="test" />
             <standard id ="12" name="test" />
         </standard>
   </standard>
</standards>

xslt:
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

      <xsl:template name="buildmenu">
              <xsl:param name="foo" />
              <xsl:choose>
                      <xsl:when test="position()!=last()">
                              <xsl:choose>
                                      <!-- test to see if foo has children 
                                              if so print header -->
                                      <xsl:when test="count($foo) != 0">
                                               has child print header 
                                              <h2><xsl:value-of 
select="@name" /></h2>
                                               <xsl:call-template 
name="buildmenu">
                                                      <xsl:with-param 
name="foo" select="$foo" />
                                              </xsl:call-template>            
                        
                                      </xsl:when>
                                      <xsl:otherwise>
                                              <!-- no children print link -->
                                              <a><xsl:attribute

name="href">addlinkinfol8r</xsl:attribute><xsl:value-of
select="@name" /></a>
                                               <xsl:call-template 
name="buildmenu">
                                                      <xsl:with-param 
name="foo" select="$foo" />
                                              </xsl:call-template> 
                                      </xsl:otherwise>
                              </xsl:choose>
                      </xsl:when>
                      <xsl:otherwise>
                              <a><xsl:attribute

name="href">addlinkinfol8r</xsl:attribute><xsl:value-of
select="@name" /></a>                 
                      </xsl:otherwise>
              </xsl:choose>
      </xsl:template>
      
      <xsl:template match="standards">
              <xsl:call-template name="buildmenu">
                      <xsl:with-param name="foo" select="standard" />
              </xsl:call-template>
      </xsl:template> 
      
       <!--<xsl:template match="standards">
              <xsl:for-each select="standard">
                              <a><xsl:attribute

name="href">addlinkinfol8r</xsl:attribute><xsl:value-of
select="@name" /></a>                 
              </xsl:for-each>
      </xsl:template> -->

</xsl:transform>      

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



__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

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



<Prev in Thread] Current Thread [Next in Thread>