Module LibXML::XML::XPath
In: ext/libxml/libxml.c

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 Namespaces

Finding namespaced elements and attributes can be tricky. Lets work through some 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::Context
Class LibXML::XML::XPath::InvalidPath
Class LibXML::XML::XPath::Object

Constants

UNDEFINED = INT2NUM(XPATH_UNDEFINED)
NODESET = INT2NUM(XPATH_NODESET)
BOOLEAN = INT2NUM(XPATH_BOOLEAN)
NUMBER = INT2NUM(XPATH_NUMBER)
STRING = INT2NUM(XPATH_STRING)
POINT = INT2NUM(XPATH_POINT)
RANGE = INT2NUM(XPATH_RANGE)
LOCATIONSET = INT2NUM(XPATH_LOCATIONSET)
USERS = INT2NUM(XPATH_USERS)
XSLT_TREE = INT2NUM(XPATH_XSLT_TREE)

[Validate]