Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM Functions : DOMXPath->query()

DOMXPath->query()

Evaluates the given XPath expression ()

Example 545. Getting all the english books

<?php

$doc
= new DOMDocument;

// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;

$doc->Load('book.xml');

$xpath = new DOMXPath($doc);

// We starts from the root element
$query = '//book/chapter/para/informaltable/tgroup/tbody/row/entry[. = "en"]';

$entries = $xpath->query($query);

foreach (
$entries as $entry) {
   echo
"Found {$entry->previousSibling->previousSibling->nodeValue}," .
       
" by {$entry->previousSibling->nodeValue}\n";
}
?>

The above example will output:

Found The Grapes of Wrath, by John Steinbeck
Found The Pearl, by John Steinbeck

We can also use the contextnode parameter to shorten our expression:

<?php

$doc
= new DOMDocument;
$doc->preserveWhiteSpace = false;

$doc->load('book.xml');

$xpath = new DOMXPath($doc);

$tbody = $doc->getElementsByTagName('tbody')->item(0);

// our query is relative to the tbody node
$query = 'row/entry[. = "en"]';

$entries = $xpath->query($query, $tbody);

foreach (
$entries as $entry) {
   echo
"Found {$entry->previousSibling->previousSibling->nodeValue}," .
       
" by {$entry->previousSibling->nodeValue}\n";
}
?>

Code Examples / Notes » dom_domxpath_query

jakob dot voss

You can transform the result nodes into new DOMDocument objects this way:
<?php
$result = $xpath->query($query);
$resultNode = $result->item(0);
$newDom = new DOMDocument;
$newDom->appendChild($newDom->importNode($resultNode,1));
print "<pre>" . htmlspecialchars($newDom->saveXML()) . "</pre>";
?>


ondrej dot fischer

Unfortunately PHP's DOM extension doesn't support use of:
<?xml-stylesheet type="text/xsl" ... ?>
processing instruction.
Here is an example, how to implement it using XPath query and extending DOMDocument by a method output().
<?php
// This simple function adds missing direct usage of anonymous instances
// in PHP5's reference model
function a($var) {
  return $var;
}
// Extended DOMDocument class
class MyDOMDocument extends DOMDocument
{
  public function output()
  {
     $stylesheets = array();
     $PIs = a(new DOMXPath($this))
                 ->query('/processing-instruction("xml-stylesheet")');
     foreach($PIs as $PI)
     {
        // This might be implemented cleaner by regular parsing
        // of DOMProcessingInstruction::data property
        if(ereg('type *= *"text/xsl" +href *= *"([^"]+)"', $PI->data, $mem))
        {
           // Here should be verified, that XSL file exists.
           a($stylesheets[] = new DOMDocument())->load($mem[1]);
        }
     }
     if($stylesheets)
     {
        $processor = new XSLTProcessor();
        foreach($stylesheets as $stylesheet)
           $processor->importStylesheet($stylesheet);
        return $processor->transformToDoc($this);
     }
     // If no stylesheet instructions present, return self directly
     else return $this;
  }
}
?>
Usage:
<?php
$document = new MyDOMDocument();
$document->load('my.xml');
echo $document->output()->saveXML();
?>
With following file my.xml:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="my.xsl" ?>
<my-root />
and existing file my.xsl that code will transform the xml file using my.xsl and output the result.


eric hanson

Two great XPath references follow.
XPath in Five Paragraphs (finally!):
http://www.rpbourret.com/xml/XPathIn5.htm
The w3c spec actually has a bunch of helpful examples:
http://www.w3.org/TR/xpath#location-paths


ckrack

tried finding a node by it's text content?
// Get all elements that equal the string "test"
$query = "//*[.='test']";


jbarnett

The order of nodes in the return value is not guaranteed.
When my code was on an old server, the returned DOMNodeList was in document order.  On the new server, the returned DOMNodeList is in a consistent order, but it is not in document order.  
PHP passes this function call off to the xmlXPathEvalExpression() function in libxml.  That function in libxml only accepts two arguments - the same two this PHP function accepts.  There must have been a change in the libxml version from the old server to the new server, and that libxml behaves differently.
This would be okay if PHP had a way to compare nodes so I can resort the nodes manually, but there is not.
So, there is no guaranteed way to get an ordered list of nodes like DOM 3 XPath provides.


nicolas_rainardnospam

Please note that what clochix says is valid for *any* document which has a default namespace (as it is the case for XHTML).
This document :
<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns="http://www.exemple.org/namespace">
<element id="1">
...
</element>
<element id="2">
...
</element>
</element>
must be accessed this way :
$document = new DOMDocument();
$document->load('document.xml');
$xpath = new DOMXPath($document);
$xpath->registerNameSpace('fakeprefix', 'http://www.exemple.org/namespace');
$elements = $xpath->query('//fakeprefix:element');
Of course, there is no prefix in the original document, but the DOMXPath class *needs* one, whatever it is, if you use a default namespace. It *doesn't work* if you specify an empty prefix like this :
$xpath->registerNameSpace('', 'http://www.exemple.org/namespace');
Hope this help to spare some time...


hayley watson

Note that if your DOMDocument was loaded from HTML, where element and attribute names are case-insensitive, the DOM parser converts them all to lower-case, so your XPath queries will have to as well; '//A/@HREF' won't find anything even if the original HTML contained "<A HREF='example.com'>".

clochix

If you want to perform queries on XHTML documents, you must fix a default namespace:
$doc = new DOMDocument;
$doc->preserveWhiteSpace = true;
$doc->resolveExternals = true; // for character entities
$doc->load("http://www.w3.org/");
$xpath = new DOMXPath($doc);
// won't work
$entries = $xpath->query("//div");
// you should use :
$xpath->registerNamespace("html", "http://www.w3.org/1999/xhtml");
$entries = $xpath->query("//html:div");


Change Language


Follow Navioo On Twitter
DOMAttr->__construct()
DOMAttr->isId()
DOMCharacterData->appendData()
DOMCharacterData->deleteData()
DOMCharacterData->insertData()
DOMCharacterData->replaceData()
DOMCharacterData->substringData()
DOMComment->__construct()
DOMDocument->__construct()
DOMDocument->createAttribute()
DOMDocument->createAttributeNS()
DOMDocument->createCDATASection()
DOMDocument->createComment()
DOMDocument->createDocumentFragment()
DOMDocument->createElement()
DOMDocument->createElementNS()
DOMDocument->createEntityReference()
DOMDocument->createProcessingInstruction()
DOMDocument->createTextNode()
DOMDocument->getElementById()
DOMDocument->getElementsByTagName()
DOMDocument->getElementsByTagNameNS()
DOMDocument->importNode()
DOMDocument->load()
DOMDocument->loadHTML()
DOMDocument->loadHTMLFile()
DOMDocument->loadXML()
DOMDocument->normalizeDocument()
DOMDocument->registerNodeClass()
DOMDocument->relaxNGValidate()
DOMDocument->relaxNGValidateSource()
DOMDocument->save()
DOMDocument->saveHTML()
DOMDocument->saveHTMLFile()
DOMDocument->saveXML()
DOMDocument->schemaValidate()
DOMDocument->schemaValidateSource()
DOMDocument->validate()
DOMDocument->xinclude()
DOMDocumentFragment->appendXML()
DOMElement->__construct()
DOMElement->getAttribute()
DOMElement->getAttributeNode()
DOMElement->getAttributeNodeNS()
DOMElement->getAttributeNS()
DOMElement->getElementsByTagName()
DOMElement->getElementsByTagNameNS()
DOMElement->hasAttribute()
DOMElement->hasAttributeNS()
DOMElement->removeAttribute()
DOMElement->removeAttributeNode()
DOMElement->removeAttributeNS()
DOMElement->setAttribute()
DOMElement->setAttributeNode()
DOMElement->setAttributeNodeNS()
DOMElement->setAttributeNS()
DOMElement->setIdAttribute()
DOMElement->setIdAttributeNode()
DOMElement->setIdAttributeNS()
DOMEntityReference->__construct()
DOMImplementation->__construct()
DOMImplementation->createDocument()
DOMImplementation->createDocumentType()
DOMImplementation->hasFeature()
DOMNamedNodeMap->getNamedItem()
DOMNamedNodeMap->getNamedItemNS()
DOMNamedNodeMap->item()
DOMNode->appendChild()
DOMNode->cloneNode()
DOMNode->hasAttributes()
DOMNode->hasChildNodes()
DOMNode->insertBefore()
DOMNode->isDefaultNamespace()
DOMNode->isSameNode()
DOMNode->isSupported()
DOMNode->lookupNamespaceURI()
DOMNode->lookupPrefix()
DOMNode->normalize()
DOMNode->removeChild()
DOMNode->replaceChild()
DOMNodelist->item()
DOMProcessingInstruction->__construct()
DOMText->__construct()
DOMText->isWhitespaceInElementContent()
DOMText->splitText()
DOMXPath->__construct()
DOMXPath->evaluate()
DOMXPath->query()
DOMXPath->registerNamespace()
dom_import_simplexml
eXTReMe Tracker