The XML::XPath module is used to query XML documents. It is usually accessed via the XML::Document#find or XML::Node#find methods. For example:
document.find('/foo', namespaces) -> XML::XPath::Object
The optional namespaces parameter can be a string, array or hash table.
document.find('/foo', 'xlink:http://www.w3.org/1999/xlink')
document.find('/foo', ['xlink:http://www.w3.org/1999/xlink',
'xi:http://www.w3.org/2001/XInclude')
document.find('/foo', 'xlink' => 'http://www.w3.org/1999/xlink',
'xi' => 'http://www.w3.org/2001/XInclude')
Working With Default Namespaces
Finding namespaced elements and attributes can be tricky. Lets work through an example of a document with a default namespace:
<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title type="text">Phil Bogle's Contacts</title> </feed>
To find nodes you must define the atom namespace for libxml. One way to do this is:
node = doc.find('atom:title', 'atom:http://www.w3.org/2005/Atom')
Alternatively, you can register the default namespace like this:
doc.root.namespaces.default_prefix = 'atom'
node = doc.find('atom:title')
More Complex Namespace Examples
Lets work through some more complex examples using the following xml document:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getManufacturerNamesResponse xmlns="http://services.somewhere.com">
<IDAndNameList xmlns="http://services.somewhere.com">
<ns1:IdAndName xmlns:ns1="http://domain.somewhere.com"/>
</IDAndNameList>
</getManufacturerNamesResponse>
</soap:Envelope>
# Since the soap namespace is defined on the root
# node we can directly use it.
doc.find('/soap:Envelope')
# Since the ns1 namespace is not defined on the root node
# we have to first register it with the xpath engine.
doc.find('//ns1:IdAndName',
'ns1:http://domain.somewhere.com')
# Since the getManufacturerNamesResponse element uses a default
# namespace we first have to give it a prefix and register
# it with the xpath engine.
doc.find('//ns:getManufacturerNamesResponse',
'ns:http://services.somewhere.com')
# Here is an example showing a complex namespace aware
# xpath expression.
doc.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse/ns0:IDAndNameList/ns1:IdAndName',
['ns0:http://services.somewhere.com', 'ns1:http://domain.somewhere.com'])
Classes and Modules
Class LibXML::XML::XPath::ContextClass LibXML::XML::XPath::Expression
Class LibXML::XML::XPath::Object
Constants
| UNDEFINED | = | 0 | Undefined value. | |
| NODESET | = | 1 | A nodeset, will be wrapped by XPath Object. | |
| BOOLEAN | = | 2 | A boolean value. | |
| NUMBER | = | 3 | A numeric value. | |
| STRING | = | 4 | A string value. | |
| POINT | = | 5 | An xpointer point | |
| RANGE | = | 6 | An xpointer range | |
| LOCATIONSET | = | 7 | An xpointer location set | |
| USERS | = | 8 | XPath user type | |
| XSLT_TREE | = | 9 | An XSLT value tree, non modifiable |