You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Guoliang Cao <ca...@ispsoft.com> on 2001/01/11 00:29:32 UTC

Can I use recursive template with parameters? files are attached.

I'm creating a recursive template with a parameter whose init value is
passed from other template.

What I want to do is to find all b's children whose position is
a_index.  It didn't work on my system.  If I didn't use this recursive
template, the value can be passed w/o any problem.  Did I break
something here?

Help appreciated!
Guoliang

<?xml version="1.0"?>
<xsl:stylesheet
xmlns:req='http://www.ispsoft.com/2000/XMLSchema/ProvRequestFormats'
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:strip-space elements="*"/>

<xsl:template match="//a">
  <xsl:apply-templates select="b" mode="recursive_b">
    <xsl:with-param name="a_index" select="2"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="b" mode="recursive_b">
  <xsl:param name="a_index"/>
  <xsl:copy>
  <xsl:apply-templates select="*[$a_index]" mode="recursive_b">
<!--  note: calling recursive template here with param -->
     <xsl:with-param name="a_index" select="$a_index"/>
  </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>


Expected output:
<b><c><c_value>11</c_value></c></b>
<b><c><c_value>102</c_value></c></b>

Real output:
<b/>
<b>101102103</b>



Re: Can I use recursive template with parameters? files are attached.

Posted by Gary L Peskin <ga...@firstech.com>.
Guoliang Cao wrote:
> 
> Sorry about that.
> 
> There is no problem with recursive templates.  In my real file, I'm using code like
> following which causes the problem.
> 
>                      I can't pass value to a template matching "/", weird!!!
>                                            v
> <xsl:apply-templates select="/" mode="mode_a">
>   <xsl:with-param name="param_a" select="position()"/>
> </xsl:apply-templates>
> 
> Guoliang

You should be able to do this as well.  What does your complete
stylesheet look like?

Gary

Re: Can I use recursive template with parameters? files are attached.

Posted by Guoliang Cao <ca...@ispsoft.com>.
Sorry about that.

There is no problem with recursive templates.  In my real file, I'm using code like
following which causes the problem.

                     I can't pass value to a template matching "/", weird!!!
                                           v
<xsl:apply-templates select="/" mode="mode_a">
  <xsl:with-param name="param_a" select="position()"/>
</xsl:apply-templates>

Guoliang

> I'm creating a recursive template with a parameter whose init value is
> passed from other template.
>
> What I want to do is to find all b's children whose position is
> a_index.  It didn't work on my system.  If I didn't use this recursive
> template, the value can be passed w/o any problem.  Did I break
> something here?
>
> Help appreciated!
> Guoliang
>
> <?xml version="1.0"?>
> <xsl:stylesheet
> xmlns:req='http://www.ispsoft.com/2000/XMLSchema/ProvRequestFormats'
>     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>     version="1.0">
>     <xsl:strip-space elements="*"/>
>
> <xsl:template match="//a">
>   <xsl:apply-templates select="b" mode="recursive_b">
>     <xsl:with-param name="a_index" select="2"/>
>   </xsl:apply-templates>
> </xsl:template>
>
> <xsl:template match="b" mode="recursive_b">
>   <xsl:param name="a_index"/>
>   <xsl:copy>
>   <xsl:apply-templates select="*[$a_index]" mode="recursive_b">
> <!--  note: calling recursive template here with param -->
>      <xsl:with-param name="a_index" select="$a_index"/>
>   </xsl:apply-templates>
>   </xsl:copy>
> </xsl:template>
>
> </xsl:stylesheet>
>
> Expected output:
> <b><c><c_value>11</c_value></c></b>
> <b><c><c_value>102</c_value></c></b>
>
> Real output:
> <b/>
> <b>101102103</b>
>
>   ------------------------------------------------------------------------
> <root>
>    <a0/>
>    <a>
>      <b>
>        <c>
>          <c_value>10</c_value>
>          <c_value>11</c_value>
>        </c>
>      </b>
>      <b>
>        <c>
>          <c_value>100</c_value>
>        </c>
>        <c>
>          <c_value>101</c_value>
>          <c_value>102</c_value>
>          <c_value>103</c_value>
>        </c>
>        <c>
>          <c_value>104</c_value>
>          <c_value>105</c_value>
>        </c>
>      </b>
>    </a>
> </root>
>
>   ------------------------------------------------------------------------
> <?xml version="1.0"?>
> <xsl:stylesheet xmlns:req='http://www.ispsoft.com/2000/XMLSchema/ProvRequestFormats'
>     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>     version="1.0">
>     <xsl:strip-space elements="*"/>
>
> <xsl:template match="//a">
>   <xsl:apply-templates select="b" mode="recursive_b">
>     <xsl:with-param name="a_index" select="2"/>
>   </xsl:apply-templates>
> </xsl:template>
>
> <xsl:template match="b" mode="recursive_b">
>   <xsl:param name="a_index"/>
>   <xsl:copy>
>   <xsl:apply-templates select="*[$a_index]" mode="recursive_b">
>      <xsl:with-param name="a_index" select="$a_index"/>
>   </xsl:apply-templates>
>   </xsl:copy>
> </xsl:template>
>
> </xsl:stylesheet>


Re: Can I use recursive template with parameters? files are attached.

Posted by Gary L Peskin <ga...@firstech.com>.
Guoliang Cao wrote:
> 
> I'm creating a recursive template with a parameter whose init value is
> passed from other template.
> 
> What I want to do is to find all b's children whose position is
> a_index.  It didn't work on my system.  If I didn't use this recursive
> template, the value can be passed w/o any problem.  Did I break
> something here?
> 
> Help appreciated!

Guoliang --

The result that is being produced is correct.  The //a template is
selecting both of the <b> children.  The b template matches only <b>
elements so it will not match "c" or "c_value" elements.  For these, the
default templates are used.

The apply-templates inside your b template has an effective select of
*[2].  This matches the second child of the b context node.  Only the
second b has a second child which is why the first b results in an
output of <b/>.

On the second b, the second child, which is a c node, is matched.  Since
there is no template for the c node, the default is used which basically
just copies all of the text node descendants (101, 102, 103) to the
result tree.

Gary