Question

I have a XSLT. which calls a function that returns a string. Like this:

<xsl:param name="mystr" select="myStrfunc()"></xsl:param>

it also calls a XML template like so:

 <li>
<xsl:call-template name="myLinks">
<xsl:with-param name="link" select="$links/link[@Id='link1']" />
<xsl:with-param name="mystr"/>
</li>

the xml template snippet looks like this:

    <link Id="link1">
     <a href="" text="click" />
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
how can I access the parameter mystr declared in xslt here??
    </link>

how can I access parameter mystr in the xml file?

Was it helpful?

Solution

<li>
  <xsl:call-template name="myLinks">
    <xsl:with-param name="link" select="$links/link[@Id='link1']" />
    <xsl:with-param name="mystr"/>
</li>

This is malformed XML -- the element xsl:call-template isn't closed.

Most probably you wanted:

<li>
 <xsl:call-template name="myLinks">
   <xsl:with-param name="link" select="$links/link[@Id='link1']" />
   <xsl:with-param name="mystr"/>
 </xsl:call-template/>
</li>

This invokes the template named "myLinks" and passes to it two parameters, named: "link" and "mystr".

However, there is no value (in a select attribute or in the body of the element) defined for the "mystr" parameter.

If you want to provide a value for the parameter -- for example the result of executing a function, this can be done as follows:

<li>
 <xsl:call-template name="myLinks">
   <xsl:with-param name="link" select="$links/link[@Id='link1']" />
   <xsl:with-param name="mystr" select="myStrfunc()"/>
 </xsl:call-template/>
</li>

Then in the body of the called template, the value of the parameter can be accessed simply by referencing it:

<link Id="link1">
 <a href="" text="click" />
 <xsl:value-of select="$mystr"/>
</link>

Or, if you need to use $mystr to generate the value of the href attribute, then do:

<link Id="link1">
 <a href="{$mystr}" text="click" />
</link>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top