Home > Back-end >  How to Use Functions in Xpath 1.0
How to Use Functions in Xpath 1.0

Time:12-13

Im using xmllint --xpath which supports only XPath 1.0. I know XPath 1.0 supports functions like concat() : https://www.w3.org/TR/1999/REC-xpath-19991116/

But I get errors like below when I try to use them in an XPath in orders to extract content from an xml document :

xmllint --debug --xpath "//*[local-name()='artifactId' and contains(text(),'log4j')]/../concat(groupId/text(),' ',artifactId/text(),' ', version/text())" ~/aax1

XPath error : Invalid expression
//*[local-name()='artifactId' and contains(text(),'log4j')]/../concat(groupId/text(),' ',artifactId/text(),' ', version/text())

xmlXPathEval: evaluation failed
XPath evaluation failure

I was reading the XPath specification. It is not clear how these functions can be used in an XPath expression ( It is clear how to use them in XSLT ). Is it possible to use them in XPath 1.0 outside the predicate ?

CodePudding user response:

XPath 1.0

A function may appear within a predicate, as you observe.

A function my also appear as a top-level expression, which can help in some situations, e.g,

concat(xp1, 'string', xp2)

where xp1 and xp2 are globally specified XPaths.

XPath 2.0

A function may appear at the last step in an XPath expression:

/e1/concat(e2, 'suffix')

See also

  • Related