xmlTreeXPathSetContext

SYNOPSIS

xmlNode xmlTreeXPathSetContext(xmlXPath xpath, xmlNode node)

Parameters:

  • xpath - the xmlXPath to set a context for

  • node - the new starting context for xpath

Returns:

  • the previous context


DESCRIPTION
xmlTreeXPathSetContext() lets you set a starting context for the xpath. This lets you perform relative XPath queries, starting from that node, instead of the document root.

Absolute queries (that start with /) are not affected by the context. The context is the xmlDoc by default, and can be reset by passing the xmlDoc to xmlTreeXPathSetContext.


EXAMPLE

<capture>
<?xml version="1.0"?>
<results>
    <result>
        <data type="name">Result 1</data>
        <data type="location">http://www.example.com/one</data>
    </result>
    <result>
        <data type="name">Result 2</data>
        <data type="location">http://www.example.com/two</data>
    </result>
</results>
</capture><$xml = $ret>
    <$doc = (xmlTreeNewDocFromString($xml, 'XML_PARSE_NOBLANKS'))>
    <$root = (xmlTreeGetRootElement($doc))>
    <$firstResult = (xmlTreeGetFirstChild($root))>
    <$xpath = (xmlTreeNewXPath($doc))>
    <$ret = (xmlTreeXPathSetContext($xpath, $firstResult))>
    <capture>data[@type='name']</capture>
    <$nodes = (xmlTreeXPathExecute($xpath, $ret))>
    <loop $nodes>
        <$ret = (xmlTreeGetContent($nodes))>
        <fmt "Matched %s\n" $ret>
    </loop>


SEE ALSO
xmlTreeNewXPath, xmlTreeXPathExecute


Copyright © Thunderstone Software     Last updated: Apr 15 2024
Copyright © 2024 Thunderstone Software LLC. All rights reserved.