downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMXPath::__construct> <DOMText::splitText
Last updated: Fri, 30 Oct 2009

view this page in

The DOMXPath class

Einführung

Supports XPath 1.0

Klassenbeschreibung

DOMXPath
DOMXPath {
/* Properties */
/* Methods */
__construct ( DOMDocument $doc )
mixed evaluate ( string $expression [, DOMNode $contextnode ] )
DOMNodeList query ( string $expression [, DOMNode $contextnode ] )
bool registerNamespace ( string $prefix , string $namespaceURI )
public void registerPhpFunctions ([ mixed $restrict ] )
}

Eigenschaften

document

Prop description

Inhaltsverzeichnis



DOMXPath::__construct> <DOMText::splitText
Last updated: Fri, 30 Oct 2009
 
add a note add a note User Contributed Notes
DOMXPath
david at lionhead dot nl
11-Aug-2009 01:33
When using DOMXPath and having a default namespace. Consider using an intermediate function to add the default namespace to all queries:

<?php
// The default namespace: x:xmlns="http://..."
$path="/Book/Title";
$path=preg_replace("\/([a-zA-Z])","/x:$1",$path);

// Result: /x:Book/x:Title
?>
ahsdg at NOSPAM dot pochta dot ru
22-Mar-2009 11:39
For more then one search this function will be helpfull:

<?php
class xpathExtension{
   public static function
getNodes($domDoc, $xpathString) {
       
$xp = new DOMXPath($domDoc);
       
$xp->registerNamespace('x', 'http://www.w3.org/1999/xhtml');
       
$xp->registerNamespace('xhtml', 'http://www.w3.org/1999/xhtml');
       
$xp->registerNamespace('i18n', 'http://apache.org/cocoon/i18n/2.1');

       
$ret = array();
       
$nodes = $xp->query($xpathString);
        foreach (
$nodes as $node) {
           
array_push($ret, $node);
        }
        return
$ret;
    }
}

$domDoc = new DOMDocument();
$domDoc->load("xmlFile.xml");
$xpathString = "//html/body/*[@class='text']";
$domNodeList = xpathExtension::getNodes($domDoc, $xpathString);
foreach(
$domNodeList as $domNode){
   echo
$domNode->nodeName;
}
?>

Thanks to:
https://svn.liip.ch/repos/public/okapi/core/trunk/inc/api/helpers/
xpath.php
Mark Omohundro, ajamyajax dot com
14-Dec-2008 05:15
<?php
// to retrieve selected html data, try these DomXPath examples:

$file = $DOCUMENT_ROOT. "test.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

$xpath = new DOMXpath($doc);

// example 1: for everything with an id
//$elements = $xpath->query("//*[@id]");

// example 2: for node data in a selected id
//$elements = $xpath->query("/html/body/div[@id='yourTagIdHere']");

// example 3: same as above with wildcard
$elements = $xpath->query("*/div[@id='yourTagIdHere']");

if (!
is_null($elements)) {
  foreach (
$elements as $element) {
    echo
"<br/>[". $element->nodeName. "]";

   
$nodes = $element->childNodes;
    foreach (
$nodes as $node) {
      echo
$node->nodeValue. "\n";
    }
  }
}
?>

DOMXPath::__construct> <DOMText::splitText
Last updated: Fri, 30 Oct 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites