Note that there are some explanatory texts on larger screens.

plurals
  1. POusing xpath in php does not work with file
    primarykey
    data
    text
    <p>Applying <code>xpath()</code> on SimpleXMLElement object, loaded with XML from a string works. If I load it from a file - it doesn't... I simply get an empty array. </p> <p>The depth level of the element that I am searching for is 5 (if we say that the root node is 1st level). When on xml from string - it works even with xpath expression that starts from the last level... When on xml from file - no xpath works at all.</p> <p>The ways I tried loading the xml content into the SimpleXMLElement object were (and applying the <code>xpath()</code>):</p> <p>1.</p> <pre><code>$xml = simplexml_load_file('filepath/file.xml'); $result = $xml-&gt;xpath('//Export/Product/Settings/Info[@Art="Short"]/@Something'); </code></pre> <p>2.</p> <pre><code>$xmlStr = file_get_contents('filepath/file.xml'); $xml = new SimpleXMLElement($xmlStr); $result = $xml-&gt;xpath('//Export/Product/Settings/Info[@Art="Short"]/@Something'); </code></pre> <p>Not successful, both of them... When from string just <code>$xml = simplexml_load_string($xmlStr);</code> worked fine. I looked through <a href="http://php.net/manual/en/simplexmlelement.xpath.php" rel="nofollow">THIS ARTICLE</a> and probably tried almost any suggestion from the comments that seemed reasonable for my case.</p> <p>Another strange thing I noticed is that when I write in the log the <code>$xml</code>, when it's from string I get the output (of arrays and object, etc.) from the depth level of <code>Product</code> (which is 2nd). But when I do it with <code>$xml</code> that has been loaded from file - I get the output from the root node.</p> <p>Simple representation of the XML code:</p> <pre><code>&lt;Export&gt; &lt;Product&gt; ...a lot of other elements... &lt;Settings&gt; &lt;Info Art="Short" Something="blahblahblah"/&gt; &lt;Info Art"asda" Somethig="dsad"/&gt; &lt;Info .... /&gt; ... &lt;/Settings&gt; &lt;/Product&gt; &lt;/Export&gt; </code></pre> <p>So my question is - how I get it working when I load the XML from a file, as it works when I load it from a string?</p> <p>P.S. I don't know if that is relevant with the current problem, but - when I load from string (heredoc), and the string has as first row <code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;</code> - I get error of calling <code>xpath()</code> on a <code>non-object</code>. But as soon as I remove this first row, and the string starts with the root node - everything is flawless.</p> <p><strong>UPDATE:</strong> the first <code>&lt;Export...&gt;</code> node:</p> <pre><code>&lt;Export xmlns="http://something" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://something C:/some_path/some_file.xsd" GA_Vertriebsweg="2" KomplettExport="true"&gt; </code></pre> <p><strong>UPDATE 2:</strong> The whole thing with loading from file does not work just because of the 1st attribute in <code>&lt;Export...</code> as in here: <code>&lt;Export xmlns="http://www.evelopment.de/schema/katalogexport/komplett"...&gt;</code>, so the whole problem is in the namespace.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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