Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <pre><code>$xpath-&gt;registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/'); </code></pre> <p>From what I understand they act like document definitions, and are required to identify certain XML elements.</p> <p>Does PHP actually do a request to that URL and verify if the element exists in the document definition?</p> </blockquote> <p><strong>No.</strong><br> That <a href="http://tools.ietf.org/html/rfc3986" rel="nofollow">URI</a> identifies an <a href="http://en.wikipedia.org/wiki/XML_namespace" rel="nofollow">XML namespace</a>, that represents an XML vocabulary. Such namespaces are designed to cope with different contexts using the same term with different meanings. With namespaces, a single XML file can contains tags and attribute with the same "name", that are qualified via a prefix. For example you can have a xml document like this:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:human="http://sample.xml.com/Human"&gt; &lt;title&gt;John Smith measures.&lt;/title&gt; &lt;body&gt; &lt;human:name&gt;John&lt;/human:name&gt; &lt;human:surname&gt;Smith&lt;/human:surname&gt; is &lt;human:height unit="feet"&gt;6&lt;/human:height&gt; feet tall. &lt;/body&gt; &lt;/html&gt; </code></pre> <p>In such content the "human" prefix is used to mark elements from the <strong><a href="http://sample.xml.com/Human" rel="nofollow">http://sample.xml.com/Human</a></strong> namespace and the empty string (that is the default prefix) is used to mark elements from the <strong><a href="http://www.w3.org/1999/xhtml" rel="nofollow">http://www.w3.org/1999/xhtml</a></strong> namespace. These URI are namespace identifiers, not schema locations (that can be expressed with either <a href="http://xmlwriter.net/xml_guide/doctype_declaration.shtml" rel="nofollow">DOCTYPE declaration</a> or <a href="http://www.w3.org/2001/XMLSchema-instance" rel="nofollow">XML Schema instance</a>). It's a good practice to provide proper documentation of the namespace at the location identified by the namespace URI, but it's not required (indeed the xhtml namespace URI points to the related W3C documentation, but the RSS extension you are looking for, doesn't).</p> <p>Note however that both <code>resolveExternals</code> and <code>validateOnParse</code> can affect the download of DTDs or schema definitions referred by the target xml, but <strong>not</strong> namespace documentation. By no means, any parser would download such a documentation, since it's intended for human consumption.</p> <blockquote> <pre><code>$result = $xpath-&gt;evaluate('string(//atom:entry[3]/slash:comments)'); </code></pre> <p>Could this be the reason why I get an empty string, while trying to retrieve the value of the element from a RSS feed?</p> </blockquote> <p><strong>No.</strong><br> First, check that the source xml contains the correct xmlns declarations and that it contains a <code>&lt;slash:comments&gt;</code> node inside the <strong>third</strong> atom entry (note, the third, because <strong>xpath indexing is one based</strong>, so that <code>//atom:entry[1]</code> means each entry that is the first in its own parent node, <code>//atom:entry[2]</code> the second and so on).<br> If so, I suspect that you forgot to register the <strong>atom</strong> namespace.<br> Try something like this (adapted from the users' contribution to <a href="http://www.php.net/manual/en/domxpath.registernamespace.php#91538" rel="nofollow">DOMXPath::registerNamespace</a> documentation):</p> <pre><code>$doc = new DOMDocument; $doc-&gt;loadXML($xml); // your xml string here $xpath = new DOMXPath($doc); $xpath-&gt;registerNamespace('atom', "http://www.w3.org/2005/Atom"); $xpath-&gt;registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/'); $result = $xpath-&gt;evaluate('string(//atom:entry[3]/slash:comments)'); </code></pre> <p>You can see this running at <a href="http://codepad.org/JX8RpaKu" rel="nofollow">http://codepad.org/JX8RpaKu</a></p> <p>Indeed, to use qualified xpaths, you need to register the default namespace too.</p>
    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