
![]() |
Note |
|---|---|
Always try to use Data::get() before resorting to Data::xpath() or Data::xpathString(). Data::get() is easier to use, and it tends to be more robust if the underlying XML structure happens to change. You should only use the XPath functions if you have a problem that requires the full power of the DOM. |
mixed xpath ( string $query [, DOMNode $contextnode ] )
The function Data::xpath() executes an arbitrary
XPath query on Data's XML. Data::xpath() is
essentially a thin wrapper around DOMXPath->query(),
where the query is automatically set up to act on the DOM representation
of your application's XML data.
Because it relies on XPath, Data::xpath() is much
more flexible and powerful than Data::get(). The downside is that XPath
queries tend to be more complicated than Data::get()
expressions. For example, these two function calls are
equivalent:
Data::get("smid:abcde/rel:Resource/@property");
Data::xpath("/*/*[@id='smid:abcde']/*[@rel='rel:Resource']/*[@property='media:width']");
Additionally, Data::xpath() typically returns a
DOMNodeList,
which means that you must do further work to extract the desired data as
strings.
Beyond the ability to run more complex queries, the chief
advantage of using Data:xpath() is that it works on certain
paths where Data::get() does not. If any
property, rel, or id attributes
in your expression include any ".", "/", or
":" characters, then Data::get() fails,
returning FALSE. For these sorts of situations, use
Data::xpath() or Data::xpathString()
instead.
query — Specifies a string XPath query to
execute on the DataRSS feed.
contextNode — (Optional) Specifies a DOMNode
for conducting a relative XPath query. By default, queries are
relative to the root element.
A DOMNodeList representing all nodes that match the
specified query. If your XPath query is designed to return a number or
string rather than a list of nodes, xpath() returns a
value with the expected type. For example, for a query of
"//div", xpath() returns a
DOMNodeList, but for a query like
"count(//div)", xpath() returns a
number.
If the expression fails to match any nodes, the function returns
an empty DOMNodeList.