1,105,447 Community Members

Parsing W3C XML response

Member Avatar
pritaeas
mod_pritaeas
11,318 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
pritaeas
mod_pritaeas
11,318 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
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
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: