Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>DOMXpath is affected by the load*() methods on DOMDocument. After loading a new xml or html, you need to recreate the DOMXpath instance:</p> <pre><code>$xml = '&lt;xml/&gt;'; $dom = new DOMDocument(); $dom-&gt;loadXml($xml); $xpath = new DOMXpath($dom); var_dump($xpath-&gt;document === $dom); // bool(true) $dom-&gt;loadXml($xml); var_dump($xpath-&gt;document === $dom); // bool(false) </code></pre> <p>In DOMXpath_reuser() you store a static variable and recreate the xpath depending on the file name. If you want to reuse an Xpath object, suggest extending DOMDocument. This way you only need pass the $dom variable around. It would work with a stored xml file as well with xml string or a document your are creating.</p> <p>The following class extends DOMDocument with an method xpath() that always returns a valid DOMXpath instance for it. It stores and registers the namespaces, too:</p> <pre><code>class MyDOMDocument extends DOMDocument { private $_xpath = NULL; private $_namespaces = array(); public function xpath() { // if the xpath instance is missing or not attached to the document if (is_null($this-&gt;_xpath) || $this-&gt;_xpath-&gt;document != $this) { // create a new one $this-&gt;_xpath = new DOMXpath($this); // and register the namespaces for it foreach ($this-&gt;_namespaces as $prefix =&gt; $namespace) { $this-&gt;_xpath-&gt;registerNamespace($prefix, $namespace); } } return $this-&gt;_xpath; } public function registerNamespaces(array $namespaces) { $this-&gt;_namespaces = array_merge($this-&gt;_namespaces, $namespaces); if (isset($this-&gt;_xpath)) { foreach ($namespaces as $prefix =&gt; $namespace) { $this-&gt;_xpath-&gt;registerNamespace($prefix, $namespace); } } } } $xml = &lt;&lt;&lt;'ATOM' &lt;feed xmlns="http://www.w3.org/2005/Atom"&gt; &lt;title&gt;Test&lt;/title&gt; &lt;/feed&gt; ATOM; $dom = new MyDOMDocument(); $dom-&gt;registerNamespaces( array( 'atom' =&gt; 'http://www.w3.org/2005/Atom' ) ); $dom-&gt;loadXml($xml); // created, first access var_dump($dom-&gt;xpath()-&gt;evaluate('string(/atom:feed/atom:title)', NULL, FALSE)); $dom-&gt;loadXml($xml); // recreated, connection was lost var_dump($dom-&gt;xpath()-&gt;evaluate('string(/atom:feed/atom:title)', NULL, FALSE)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload