12.10.1. ProblemYou want to make sure your XML document abides by a schema, such as XML Schema, RelaxNG, and DTDs. 12.10.2. SolutionUse the DOM extension. With existing DOM objects, call DOMDocument::schemaValidate( ) or DOMDocument::relaxNGValidate( ): $file = 'address-book.xml'; $schema = '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. With XML in a string, call DOMDocument::schemaValidateSource( ) or DOMDocument::relaxNGValidateSource( ): $xml = '<person><firstname>Adam</firstname></person>'; $schema = 'address-book.xsd'; $ab = new DOMDocument $ab->>load($file); if ($ab->>schemaValidateSource($schema)) { print "XML is valid.\n"; } else { print "XML is invalid.\n"; } 12.10.3. DiscussionSchemas are a way of defining a specification for your XML documents. While 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 RelaxNG. 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 RelaxNG are more recent schemas and attempt to solve some of the issues surrounding DTDs. PHP 5 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 RelaxNG, 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 RelaxNG validation, while 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 (see Example 12-21). 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. Validating an XML document
If the schema is stored in a string, use DOMDocument::schemaValidateSource( ) instead of schemaValidate( ). Table 12-3 lists all the validation methods.
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 RelaxNG support validation against files and strings. You can validate a DOM object only against the DTD defined at the top of the XML document. 12.10.4. See AlsoThe XML Schema specification at http://www.w3.org/XML/Schema; the Relax NG specification at http://www.relaxng.org/. |