PHP Doku:: Creates new xpath context - function.xpath-new-context.html

Verlauf / Chronik / History: (11) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDOM-XMLDOM-XML-Funktionenxpath_new_context

Ein Service von Reinhard Neidl - Webprogrammierung.

DOM-XML-Funktionen

<<xpath_eval

xpath_register_ns_auto>>

xpath_new_context

(PHP 4)

xpath_new_context Creates new xpath context

Beschreibung

XPathContext xpath_new_context ( domdocument $dom_document )

Creates a new xpath context.

Siehe auch

  • xpath_eval() - Evaluates the XPath Location Path in the given string


4 BenutzerBeiträge:
- Beiträge aktualisieren...
David
9.08.2006 12:46
WARNING FOR PHP5 USERS! (if you are having xpath problems)

There seem to be several versions of the DOM library/extension arround, it would appear that the one in this page is for PHP4 only, while this is the one in PHP5:

http://us2.php.net/manual/en/ref.dom.php

See the DOMXPath class for xpath functions.
webmaster at ebinformatique dot com
8.02.2006 7:32
Should that not be placed as:
DomDocument->xpath_new_context as it seems to be a method used from a DomDocument object.
augusto at omnitec dot it
22.06.2004 19:52
Split easily XPath result into associative array:

function xpath_parser($xml_file, $xpath_query) {

    $xml_file = realpath($xml_file);

    if (file_exists($xml_file)) {

        $doc = domxml_open_file($xml_file);

        $ctx = $doc->xpath_new_context();           

        if ($xpathObj = @$ctx->xpath_eval($xpath_query)) {

            $return = array();

            foreach($xpathObj->nodeset as $risultato) {

                $tagName = $risultato->tagname();
                $content = $risultato->get_content();

                if (array_key_exists($tagName, $return)) {

                    $return[$tagName][] = $content;

                }else {

                    if (count($doc->get_elements_by_tagname($tagName)) > 1) {

                        $return[$tagName] = array($content);

                    }else {

                        $return[$tagName] = $content;

                    }
                }
            }

        }else {  return("<b>Error :</b> Wrong XPath query");  }
    }else {  return("<b>Errore :</b> File not exist or wrong filepath!");  }

    return $return;
}

usage:

$xml_file = "./listaCD.xml";
$query = "/listacd/*";

echo "<pre>";
print_r(xpath_parser($xml_file, $query));
echo "</pre>";

27.05.2003 19:16
I found a nice function in XML_sql2xml. With it, you can get, easily,  the result of a xpath expression.

function getXpathValues(&$dom, $expr) {
    $xpth = $dom->xpath_new_context();
        $xnode = xpath_eval($xpth,$expr);
        if (isset ($xnode->nodeset[0])) {
            $firstnode = $xnode->nodeset[0];
            $children = $firstnode->children();
            $value = $children[0]->content;
        return $value;
    }
        else return Null;
}

Usage :

$dom = domxml_open_file('pear.rss');
echo $this->getXpathValues($dom, "//channel/title");



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)