1

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?

1
  • <xsl:with-param name="mystr" select="$mystr"/>? It's not super-clear what you are asking. Can you include a small self-contained example, the xml output you expect, and the xml output you are getting instead? Commented Dec 21, 2012 at 20:46

1 Answer 1

2
<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>
Sign up to request clarification or add additional context in comments.

4 Comments

I have problems doing this: <link Id="link1"> <a href="" text="click" /> <xsl:value-of select="$mystr"/> </link> In the XML file it does not allow me to have this 'xsl' tag. Also how can I just use $mystr, like so <a href='$mystr' text='click'>?
If you're trying to put an XSLT instruction into your XML file, then you are very confused indeed.
@Dimitre <a href="{$mystr}" text="click"> in xml..just renders the to the page as <a href="{$mystr}" text="click">. my xml is seeing it as a literal. any idea why?
@River, Michael Kay (the comment above) already told you why: XSLT code must be in its own XML document (aka stylesheet) -- you cannot embed it into the XML document itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.