PHP XML Validating XML Documents - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP XML Validating XML Documents - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, June 15, 2019

PHP XML Validating XML Documents

PHP XML


Validating XML Documents

Problem

You want to make sure your XML document abides by a schema, such as XML Schema,  Relax NG, and DTDs.

Solution

Use the DOM extension.

To validate a DOM object against a schema stored in a file, call DOMDocument::schema Validate() or DOMDocument::relaxNGValidate():

          $file = __DIR__ . '/address-book.xml';
          $schema = __DIR__ . '/address-book.xsd';
          $ab = new DOMDocument;
          $ab->load($file);

          if ($ab->schemaValidate($schema)) {
               print "$file is valid.\n";
          } else {
               print "$file is invalid.\n";
          }

If your XML document specifies a DTD at the top, call DOMDocument::validate() to validate it against the DTD.

To validate a DOM object against a schema stored in a variable, call DOMDocument::schemaValidateSource() or DOMDocument::relaxNGValidateSource():

          $file = __DIR__ . '/address-book.xml';
          $ab = new DOMDocument;
          $ab->load($file);

          $schema = file_get_contents(__DIR__ . '/address-book.xsd');

          if ($ab->schemaValidateSource($schema)) {
               print "XML is valid.\n";
          } else {
               print "XML is invalid.\n";
          }

Discussion

Schemas are a way of defining a specification for your XML documents. Though the goal is the same, there are multiple ways to encode a schema, each with a different syntax.

Some popular formats are DTDs (Document Type Definitions), XML Schema, and Relax NG. DTDs have been around longer, but they are not written in XML and haveother issues, so they can be difficult to work with. XML Schema and Relax NG are more recent schemas and attempt to solve some of the issues surrounding DTDs.

PHP uses the libxml2 library to provide its validation support. Therefore, it lets you validate files against all three types. It is most flexible when you’re using XML Schema and Relax NG, but its XML Schema support is incomplete. You shouldn’t run into issues in most XML Schema documents; however, you may find that libxml2 cannot handle some complex schemas or schemas that use more esoteric features.

Within PHP, the DOM extension supports DTD, XML Schema, and Relax NG validation, whereas SimpleXML provides only an XML Schema validator.

Validating any file using DOM is a similar process, regardless of the underlying schema format. To validate, call a validation method on a DOM object. For example:

          $file = __DIR__ . '/address-book.xml';
          $schema = __DIR__ . '/address-book.xsd';
          $ab = new DOMDocument;
          $ab->load($file);

          if ($ab->schemaValidate($schema)) {
               print "$file is valid.\n";
          } else {
               print "$file is invalid.\n";
          }

It returns true if the file passes. If there’s an error, it returns false and prints a message to the error log. There is no method for capturing the error message.

If the schema is stored in a string, use DOMDocument::schemaValidateSource() instead of schemaValidate().

Table  lists all the validation methods.

Table  DOM schema validation methods

Method name                     Schema type         Data location   
schemaValidate                 XML Schema        File
schemaValidateSource     XML Schema        String
relaxNGValidate                Relax NG               File
relaxNGValidateSource   Relax NG                String
validate                                DTD                        N/A
________________________________________

All of the validation methods behave in a similar manner, so you only need to switch the method name in the previous example to switch to a different validation scheme.

Both XML Schema and Relax NG support validation against files and strings. You can validate a DOM object only against the DTD defined at the top of the XML document.



No comments:

Post a Comment

Post Top Ad