Using param value in xpath

J

jose.jeria

I have the following
Code:


<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" indent="yes" encoding="UTF-8"/>
<xsl:param name="myvar"/>

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="strings/$myvar">
<p><xsl:value-of select="."/></p>
</xsl:for-each>

</body>
</html>
</xsl:template>

</xsl:stylesheet>



What i want to do is to use the $myvar in the xpath expression. Its
marked in bold, but that gives me an error. I tried the concat
function, but that did also didnt work. What am I doing wrong?
 
J

Joris Gillis

Hi,
What i want to do is to use the $myvar in the xpath expression. Its
marked in bold, but that gives me an error. I tried the concat
function, but that did also didnt work. What am I doing wrong?

Variables or parameters cannot be used in XSLT to dynamically make xpath expression.

You could use an extension function. e.g http://www.exslt.org/dyn/functions/evaluate/index.html

But I certainly wouldn't to that myself to solve your specific problem. Simple use:

'strings/*[local-name()=$myvar]'

or the lightening fast key equivalent:

<xsl:key name="node" match="strings/*" use="local-name()"/>

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('node',$myvar)">
<p><xsl:value-of select="."/></p>
</xsl:for-each>
</body>
</html>
</xsl:template>



regards,
 
M

Martin Honnen

<xsl:param name="myvar"/>
<xsl:for-each select="strings/$myvar">
What i want to do is to use the $myvar in the xpath expression. Its
marked in bold, but that gives me an error. I tried the concat
function, but that did also didnt work. What am I doing wrong?

There is no dynamic XPath expression evaluation, what you have is not
syntactically correct.
It is hard to tell you what to use instead as I have no idea what you
want to achieve, you have not provided any information what type of
value you set the variable to.
If you have for instance a variable/param of type string with an element
name and you want to select elements with that name then
<xsl:for-each select="strings/*[name() = $myvar]">
might do what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top