1.11M Members

Parsing W3C XML response

 
0
 

Below is the XML response you get from the W3C Validation API:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
  <env:Body>
    <m:markupvalidationresponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding" xmlns:m="http://www.w3.org/2005/10/markup-validator">
      <m:uri>http://www.pritaeas.net/</m:uri>
      <m:checkedby>http://validator.w3.org/</m:checkedby>
      <m:doctype>-//W3C//DTD XHTML 1.1//EN</m:doctype>
      <m:charset>utf-8</m:charset>
      <m:validity>true</m:validity>
      <m:errors>
        <m:errorcount>3</m:errorcount>
        <m:errorlist>
          <m:error>
            <m:line>10</m:line>
            <m:col>1</m:col>                                           
          </m:error>
          <m:error>
            <m:line>12</m:line>
            <m:col>2</m:col>                                           
          </m:error>
          <m:error>
            <m:line>14</m:line>
            <m:col>3</m:col>                                           
          </m:error>
        </m:errorlist>
      </m:errors>
      <m:warnings>
        <m:warningcount>0</m:warningcount>
        <m:warninglist>
        </m:warninglist>
      </m:warnings>
    </m:markupvalidationresponse>
  </env:Body>
</env:Envelope>

I've tried the following code to obtain a list of error nodes, but something is wrong or missing. The length is always reported as zero, so no elements in the list.

$doc = new DOMDocument();
$doc->loadXML(file_get_contents('check.xml'));
  
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('m', 'http://www.w3.org/2005/10/markup-validator');
  
$nodelist = $xpath->query('error');
  
print_r($nodelist);
echo '<br/>Length: ' . $nodelist->length;

Hopefully someone can enlighten me.

 
0
 

Found my own answer:

$path = 'check.xml';
  $doc = new DOMDocument();
  $doc->load($path);
  
  $xpath = new DOMXPath($doc);
  $xpath->registerNamespace('m', 'http://www.w3.org/2005/10/markup-validator');
  
  $nodelist = $xpath->query('//m:markupvalidationresponse/m:errors/m:errorlist/m:error');
  
  print_r($nodelist);
  echo '<br/>Length: ' . $nodelist->length;
  
  foreach ($nodelist as $node) {
    echo '<br/>  ' . $node->nodeValue;
  }

Had no idea I needed to specify the full path.

Question Self-Answered as of 3 Years Ago
Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: