Converting the result type of string-join to NodeList

886 Views Asked by At

I need to deal with legacy code which evaluates XPath expressions expects the results to resolve to NodeList. The code uses NodeList to identify how many nodes are selected by the XPath expression. The code proceeds only if one node is selected.

Now I need to use string-join function which return type is String. Is there any way to convert String to NodeList using another XPath function? Ideally, it will convert the String to a NodeList with one element which text content would be the String value. I tried with exsl:node-set() but with no success (Saxon-HE processor).

3

There are 3 best solutions below

1
On BEST ANSWER

Is there any way to convert String to NodeList using another XPath function?

No, XPath is just a query language for XML documents -- it doesn't create nodes.

If the legacy code expects the XPath expression to evaluate to a NodeList and the result of evaluationg your XPath expression is just a string, then to reuse that legacy code without writing new code, you must change your XPath expression, so that it selects one or more nodes.

Or, write your own code that uses the result of the evaluation of the XPath expression.

5
On

XPath (even 2.0) can't create new nodes, for that you need XQuery or XSLT. Saxon supports both so you have the choice.

0
On

I'd suggest putting a Java layer around the Saxon XPath API, and doing the string-to-NodeList conversion in this Java layer. Your Java layer can implement the JAXP XPath interface and thus "pretend" to be the real XPath engine.