xsl-list
[Top] [All Lists]

Re: [xsl] rephrased: passing parameters to generic templates

2006-04-12 01:25:28
On 4/12/06, Robert Frank <robert(_dot_)frank(_at_)unibas(_dot_)ch> wrote:
Hi,

I noticed I didn't phrase my problem carefully enough (and the example
wasn't correct either - thank's David) - probably too late at night:

I want to convert only certain (html) nodes using a generic template,
but have to pass a parameter to these templates. As the nodes may be
nested within othernodes, I am using the generic copy 'function'

<xsl:template match="@*|node()|text()" priority="-1">
    <xsl:param name="path" select="'zz'"/>
    <xsl:copy>
      <xsl:apply-templates select="@*|node()|text()">
        <xsl:with-param name="path" select="$path"/>
      </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

The more specific template is:

<xsl:template match="/content">
    <test>
      <xsl:apply-templates select="para">
        <xsl:with_param name="path" select="docBase"/>
      </xsl:apply-templates>
    </test>
</xsl:template>

and the element I want to change:

<xsl:template match="a/@href">
    <xsl:param name="path" select="'xx'"/>
    <xsl:attribute name="href"><xsl:value-of select="$path"/>
       <xsl:value-of select="."/></xsl:attribute>
</xsl:template>

A sample xml file could be:
<content>
    <para>
      <docBase>full_path_to_some_directory</docBase>
      This is a test <b>text</b> which should be copied 1:1.
      <a href=3D"local_ref">A reference</a>
      <center><a href=3D"local_ref">A nested reference</a></center>
    </para>
    <para>
      <docBase>next_full_path_to_some_directory</docBase>
      This is a test <b>text</b> which should be copied 1:1.
      <a href=3D"local_ref">A reference</a>
      <center><a href=3D"local_ref">A nested reference</a></center>
    </para>
</content>

I want to use the <docBase> in every <para> to modify the href
of each anhor element.
How can I propagate the parameter's value in the more specific
template into the generic template? The only parameter that is
propagated is the one in the generic template, so all references have
a value of zz...

No need for parameters here, just select the paras <docBase> when you
match the <a>, eg:

<xsl:template match="a">
  <a href="{ancestor::para/docBase}">.....

Be aware that using the ancestor axis is probably the slowest
solution, so if that's an issue there are faster ways to do this...

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--