Parsing W3C XML response


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

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="">
    <m:markupvalidationresponse env:encodingStyle="" xmlns:m="">
      <m:doctype>-//W3C//DTD XHTML 1.1//EN</m:doctype>

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();
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('m', '');
$nodelist = $xpath->query('error');
echo '<br/>Length: ' . $nodelist->length;

Hopefully someone can enlighten me.


Found my own answer:

$path = 'check.xml';
  $doc = new DOMDocument();
  $xpath = new DOMXPath($doc);
  $xpath->registerNamespace('m', '');
  $nodelist = $xpath->query('//m:markupvalidationresponse/m:errors/m:errorlist/m:error');
  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 4 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

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