2

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 ?

Sony Antony
  • 314
  • 1
  • 11

1 Answers1

2

XPath 1.0

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

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

concat(xp1, 'string', xp2)

where xp1 and xp2 are XPath expressions: concat() in general can take an arbitrary number of arguments, which it converts to strings and concatenates together.

XPath 2.0

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

/e1/concat(e2, 'suffix')

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thank you so much for teh response. I have been pulling my hair over this. If what you are saying is correct, then concat() in XPath 1.0 is not of much use... is it possible to combine the values from three sibling nodes, in XPath 1.0 as I was trying to do ? ( My xpath worked fine when I tested it in https://www.freeformatter.com/xpath-tester.html...which I suspect is using XPath 2+ ) – Sony Antony Dec 13 '21 at 02:56
  • 1
    Correct, XPath 1.0 has no equivalent to the string-join() function in XPath 2.0, which allows you to construct a string by concatenating values from a variable number of nodes. XPath 2.0 was invented for a reason! – Michael Kay Dec 13 '21 at 09:48