0

I have an XPath string as follows:

/results/server[@name='server1']

I would like to construct a new XPath using a previously defined XSLT variable:

/results/server[@name='$server']

I haven't tried 'concat' might be possible with that. I am using XSLT 2.0. XSLT parser complains if I use the string as defined above:

XPST0003 XPath syntax error at char 65 on line 89 near: Unexpected token "" in path expression

3
  • What do you mean, "complain"? All that looks valid to me (even if the string containing a dollar sign is probably not what you want.) Any error message? Commented Jun 18, 2015 at 17:56
  • NEVER tell us that something gives you an error (or "complains") without telling us what the error is. We like to help, but not when people seem to be wilfully holding back the relevant information. Commented Jun 18, 2015 at 20:40
  • XPST0003 XPath syntax error at char 65 on line 89 near {...q $currentHost]/hardwareSta...}: Unexpected token "<eof>" in path expression Commented Jun 18, 2015 at 20:49

1 Answer 1

2

You can refer to the variable anywhere in the XPath expression:

/results/server[@name eq $server]

If you write it with quotes, '$server', then it is just a string literal that happens to contain a dollar sign.

Sign up to request clarification or add additional context in comments.

4 Comments

I get a syntax error using that format. Here is the string: <xsl:variable name="PATH" select="/summary/results/server[hostname eq $currentHost]/hardwareStats/"/>
Your expression ends with a path operator (aka a slash), which is invalid. That is indeed a syntax error.
Thanks for your answer - I didn't think it was even possible to put XSLT variables into xpaths... based on reading this dpawson.co.uk/xsl/sect2/nono.html#d1874e40
It is different. In your question, you want to use a variable in an expression. In Dave's FAQ, it is about building an expression as a string and evaluating it dynamically (taking the specific example of an expression being a variable reference, like the string containing '$server'). Dynamic evaluation is like the eval in many languages (and xsl:evaluate in XSLT), but this is not what you want here.

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.