I'm looking for a way to concatenate two arbitrary, valid XPath expressions to build a new XPath expression.
Effectively I'd like to apply the second XPath expression on the result of the first XPath expression.
For example, take this XML:
<foo>
<bar>
<baz />
</bar>
<foo>
The XPath expression /foo
would obviously return the root element, while /bar
would return nothing. But /bar
applied to the result of /foo
should return the <bar>
element.
Now the naive implementation would be to just use String concatenation to build /foo/bar
and evaluate that to get <bar>
. This works in this specific case.
Now my question is: is it always that simple? Are there any types of XPath expressions where this won't result in a valid expression or would lead to unexpected results?
It isn't much of a problem if some obscure XPath expressions can't be validated that way, but I'd like the common ones to work.
If there are problems: Is there any kind of concatenation that works in more situations?