scozy
scozy

Reputation: 2582

Using XPath functions in libxml

I am trying to evaluate certain XPath expressions with libxml 2.8.0 and am getting error messages with functions. It is not clear to me whether libxml implements the whole or a subset of the XPath specification. Is there any resource clearly identifying what is implemented and how to use it?

In my specific case, one expression I am trying to evaluate is:

/TemporalInformation/RecipeInformation[fn:exists(./ActionToActionRelation[fn:contains(./@actionA,'cook') and fn:exists(./IngredientA[fn:contains(./@classes,'onion')])])]

I am using libxml through the XML::LibXML Perl module, but I have also tried my expression with the xpath1.c example tool available from xmlsoft.org, and got the same error messages:

$ ./xpath-tester data/fk.xml "/TemporalInformation/RecipeInformation[fn:exists(.> /ActionToActionRelation[fn:contains(./@actionA,'cook') and fn:exists(./IngredientA[fn:contains(./@classes,'onion')])])]" "fn=http://www.w3.org/2005/xpath-functions"
xmlXPathCompOpEval: function contains not found
XPath error : Unregistered function
xmlXPathCompOpEval: function exists not found
XPath error : Unregistered function
XPath error : Invalid expression
XPath error : Stack usage errror
Error: unable to evaluate xpath expression "/TemporalInformation/RecipeInformation[fn:exists(./ActionToActionRelation[fn:contains(./@actionA,'cook') and fn:exists(./IngredientA[fn:contains(./@classes,'onion')])])]"
Usage: ./xpath-tester <xml-file> <xpath-expr> [<known-ns-list>]
where <known-ns-list> is a list of known namespaces
in "<prefix1>=<href1> <prefix2>=href2> ..." format

I have tried with and without the fn namespace, both with xpath.c and my Perl script, and got the same result.

Upvotes: 1

Views: 2288

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122394

The libxml XPath implementation is only XPath 1.0, so 2.0-only functions such as exists aren't available. In 1.0 you only have a few core functions - there's starts-with but not ends-with, no regular expression support, no date handling, etc. and there's no strong typing and none of the higher-level constructs like if and for.

So to answer your question, yes, it does support the whole of XPath, but only XPath 1.0.

Upvotes: 1

Related Questions