InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Validating XML Documents in PHP
PHP

Validating XML Documents in PHP

InfinityCoder December 8, 2016

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

Use the DOM extension.

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

1
2
3
4
5
6
7
8
9
10
$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():

1
2
3
4
5
6
7
8
9
10
11
$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";
}

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 have other 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:

1
2
3
4
5
6
7
8
9
10
$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 12-3 lists all the validation methods.

Table 12-3. DOM schema validation methods

Method name Schema type Data location
schemaValidate

schemaValidateSource

relaxNGValidate

relaxNGValidateSource

validate

XML Schema

XML Schema

Relax NG

Relax NG

DTD

File

String

File

String

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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Program: HTTP Range in PHP
The program in Example 8-22 implements the HTTP Range feature, …

Program: HTTP Range in PHP

Defining Static Properties and Methods in PHP
You want to define methods in an object, and be …

Defining Static Properties and Methods in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap