Validate XML with RNG in PHP

46 Views Asked by At

I want to validate an XML file against an RNG schema. For that, I use the "DOMDocument relaxNGValidate" function. I would like all errors to be displayed. But the code I developed only displays the first one. I have searched but can't find how to display all the errors.

Here is the PHP code:

<?php
libxml_use_internal_errors(true);

// Create a new DOMDocument
$doc = new DOMDocument;

// Load XML 
$doc->load('test.xml');

// Validate XML against RNG
if ($doc->relaxNGValidate('test.rng')) {
    echo "Valid File.";
} else {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
        echo "Erreur : " . $error->message . " (ligne " . $error->line . ")\n";
    }
    libxml_clear_errors();
}

libxml_use_internal_errors(false);
?>

Only the first error appears. Here's an example XML file :

<?xml version="1.0"?>
<collegqe>
    <rollno>
        <name>John Smith</name>
        <subject>Web</subject>
    </rollno>
    <rollno>
        <nameq>John Doe</nameq>
        <subject>CSE</subject>
    </rollno>
</collegqe> 

and the RNG file :

<grammar xmlns="http://relaxng.org/ns/structure/1.0">
  <start>
    <element name="college">
      <zeroOrMore>
        <element name="rollno">
          <element name="name">
            <text/>
          </element>
          <element name="subject">
            <text/>
          </element>
        </element>
      </zeroOrMore>
    </element>
  </start>
</grammar>

Maybe I'm using the function incorrectly. Perhaps I could transform the rng into xsd before doing the check?

0

There are 0 best solutions below