xmlNode xmlTreeXPathExecute(xmlXPath xpath, string expression)


  • xpath - an xmlXPath object

  • expression - the XPath expression to execute


  • the xmlNode's that match the XPath expression

xmlTreeXPathExecute() executes an XPath expression, returning all xmlNode's that match the expression. It operates on an xmlXPath object, which can be obtained from a doc with xmlTreeNewXPath().

If the specified XPath returns a basic data type such as a string (like name(/*)) or a number (like string-length(name(/*)), then it will be contained in a single XML_TEXT_NODE.

This example finds all <item>s with a price attribute that's greater than 5, and prints out their <name> sub-element and price attribute.

<$xpath = (xmlTreeNewXPath($doc))>
    <$nodes = (xmlTreeXPathExecute($xpath, '//item[@price>5]'))>
    <loop $nodes>
        <$name = (xmlTreeGetChildrenContent($nodes, 'name'))>
        <$price = (xmlTreeGetAttributeContent($nodes, 'price'))>
        $name costs $price

In the unusual case that you're XPathing for a namespace declaration directly (//*/namespace::myprefix), note that the xmlNs object returned is a copy, and changes made to it will not be reflected in the original doc (elements and attributes return a reference in the original doc, and do not have this limitation).

Instead, you can XPath for the element that contains that definition (//*[namespace::myprefix]), use xmlTreeGetNsDef to get the namespace object and change it as needed.

xmlTreeNewXPath, xmlTreeXPathRegisterNs xmlTreeXPathSetContext

Copyright © Thunderstone Software     Last updated: Oct 24 2023
Copyright © 2024 Thunderstone Software LLC. All rights reserved.