Xpath and parameters
I am relatively new to XSL and I think this is a basic question. So I better get my apologies in early!
Basically, I would like to use a value-of function. This will be in a template and I would like to pass part of the xpath as a parameter.
<xsl:variable name="TEST_VAR">"h:elementA/elementB"</xsl:variable>
... and then use the variable (or passed in param) as all (o开发者_StackOverflowr part) of the xpath:
<xsl:element name="transformedElement"><xsl:value-of select=$TEST_VAR/></xsl:element>
I would really like to have the xpath to be a mixture of string literals and the vlue of TEST_VAR.
Thanks in advance
What you want to do can't be done. You cannot interpret plain strings as XPath expressions within an XSL transform.
But you shouldn't have to, anyway. By the looks of it you can replace your example with the following to get what you want:
<xsl:variable name="TEST_VAR" select="h:elementA/elementB"/>
<xsl:element name="transformedElement">
<xsl:value-of select="$TEST_VAR"/>
</xsl:element>
But I suspect you've dumbed down your example to the point that it's too simple to see what you actually want.
Here's a quick tip for using Stack Overflow: phrase your questions about what you actually need to do, and why you're having trouble doing it. What you've done with this question is thought of a solution to your problem, and are asking Stack Overflow if it's possible. It's not, and since I can't see your original problem I can't help you more than I already have.
Next time you ask a question, make sure to actually ask a question about the problem you're facing, not the potential solution to the problem you're facing. You'll get better answers that way.
Pass the desired XPath expression as a global <xsl:param>
. If the name of this global parameter is vQuery
then you can use code like this:
<xsl:element name="transformedElement">
<xsl:value-of select=
"*[name() = substring-before($vQuery, '/')]
/*[name() = substring-after($vQuery, '/')]"/>
</xsl:element>
Such technique generally requires that you have a lot of knowledge about the XPath expression that will be passed. In this case it relies on the fact that this is a relative XPath expression with exactly two location steps and no predicates in them.
精彩评论