Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>DOMXpath</code> class (instead of XSLTProcessor in <a href="https://stackoverflow.com/questions/18086094/php-domdocument-reuse-of-xsltprocessor-it-is-stable-secure">your another question</a>) use reference to given <code>DOMDocument</code> object in contructor. <code>DOMXpath</code> create <code>libxml</code> context object based on given <code>DOMDocument</code> and save it to internal class data. Besides <code>libxml</code> context it<code>s saves references to original</code>DOMDocument` given in contructor arguments. </p> <p><strong>What that means:</strong></p> <p>Part of sample from <a href="https://stackoverflow.com/users/2265374/thomasweinert">ThomasWeinert</a> answer:</p> <pre><code>var_dump($xpath-&gt;document === $dom); // bool(true) $dom-&gt;loadXml($xml); var_dump($xpath-&gt;document === $dom); // bool(false) </code></pre> <p>gives false after load becouse of <code>$dom</code> already holds pointer to new <code>libxml</code> data but <code>DOMXpath</code> holds <code>libxml</code> context for <code>$dom</code> before load and pointer to real document after load. </p> <p><strong>Now about <code>query</code> works</strong></p> <p>If it should return <code>XPATH_NODESET</code> (as in your case) its make a node copy - node by node iterating throw detected node set(<code>\ext\dom\xpath.c</code> from 468 line). <strong>Copy but with original document node as parent</strong>. Its means that you can modify result but this gone away you XPath and DOMDocument connection.</p> <p>XPath results provide a parentNode memeber that knows their origin:</p> <ul> <li>for attribute values, parentNode returns the element that carries them. An example is //foo/@attribute, where the parent would be a foo Element.</li> <li>for the text() function (as in //text()), it returns the element that contains the text or tail that was returned.</li> <li>note that parentNode may not always return an element. For example, the XPath functions string() and concat() will construct strings that do not have an origin. For them, parentNode will return None.</li> </ul> <p>So, </p> <ol> <li>There is no any reasons to cache <code>XPath</code>. It do not anything besides <code>xmlXPathNewContext</code> (just allocate lightweight <a href="http://xmlsoft.org/html/libxml-xpath.html#xmlXPathContext" rel="nofollow noreferrer">internal struct</a>). </li> <li>Each time your modify your <code>DOMDocument</code> (removeChild, replaceChild, etc.) your should recreate <code>XPath</code>. </li> <li>We can not use something like normalizeDocument to "refresh DOM" because of it change internal document structure and invalidate <code>xmlXPathNewContext</code> created in <code>Xpath</code> constructor.</li> <li>Only "new DOMXpath($doc);" is allways secure? Yes, if you do not change $doc between <code>Xpath</code> usage. Need to reload $doc also - no, because of it invalidated previously created <code>xmlXPathNewContext</code>.</li> </ol>
    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. 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.
    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