How to Validate XML File against a Schema using PHP

Validating an XML file against a schema is essential to have control over the way the elements are arranged in the XML file. PHP has functions to perform this task. You can use the schemaValidate function to validate the XML file against a schema. This functions take the schema filename as a parameter.


_______________________________________________


Consider an XML file named candidate.xml which is a valid file against a schema is given below:

<?xml version="1.0" encoding="UTF-8"?>
<candidate>
<candidatename id='candid'>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>XYZ Corp</organization>
</candidate>

The schema for this file is given in the file candidate.xsd. The schema is given as below:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="SSN" type="xs:int"/>
<xs:element name="organization" type="xs:string"/>
<xs:element name="candidate">
<xs:complexType>
<xs:sequence>
<xs:element ref="candidatename"/>
<xs:element ref="candidatecity"/>
<xs:element ref="SSN"/>
<xs:element ref="organization"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="candidatecity" type="xs:string"/>
<xs:element name="candidatename">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>

The code to validate the XML file against the schema is given below.

<?php
$xdoc = new DomDocument;
$xmlfile = 'C:/php/xmlfiles/candidate.xml';
$xmlschema = 'C:/php/xmlfiles/candidate.xsd';
//Load the xml document in the DOMDocument object
$xdoc->Load($xmlfile);
//Validate the XML file against the schema
if ($xdoc->schemaValidate($xmlschema)) {
print "$xmlfile is valid.\n";
} else {
print "$xmlfile is invalid.\n";
}
?>

In the above code we are creating a DOMDocument first and loading the xml file into that DOMDocument object. Then we use the schemaValidate function to validate that document in the DOMDocument object. We use the if condition to print out the result of the validation. We use two variables called the $xmlfile and $xmlschema to store the values of the path to the XML file and the Schema file.

The output of the above code is given in the figure shown below:



_______________________________________________

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________



 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

Copyright - © 2004 - 2017 - All Rights Reserved.