I have created an xml like below
<Request>
<RequestType>Logon</RequestType>
<MobileId>23424</MobileId>
<Password>123456Gg</Password>
</Request>
and my xsd file is like below code
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Request" type="RequestType"/>
<xsd:complexType name="RequestType">
<xsd:sequence>
<xsd:element name="RequestType">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Logon"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="MobileId" >
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="0" />
<xsd:maxLength value="10" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Password">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="0"/>
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
I have used PHP'S DOMDocument's schemaValidate function to validate the xml against the xsd, and it gives following error
Fatal Error 4: Start tag expected, '<' not found on line 5
Error 1872: The document has no document element. on line 0
But I have tested those two files (xml and xsd) in this link W3C XML Schema Online validation, and it successfully validates without showing any error.
What I have to do to get work this in php?
Note: my php libxml version is 2.7.8
Simply go to the XML Tools > Validate Now option and click on it. You can also press Ctrl + Alt + Shift + M key combination to open Validate Now option. Now, select the XSD file against which you want to validate the opened XML document. Simply browse and then import the XSD file in the respective field.
The DOMDocument::validate() function is an inbuilt function in PHP which is used to validate the document based on its DTD (Document Type Definition). DTD defines the rules or structure to be followed by the XML file and if a XML document doesn't follows this format then this function will return false.
You can validate your XML documents against XML schemas only; validation against DTDs is not supported. However, although you cannot validate against DTDs, you can insert documents that contain a DOCTYPE or that refer to DTDs.
Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation. Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.
dom specially gives two functions to validate with schema. One is to give file path
$doc = new DOMDocument();
$doc->load('PATH TO XML');
$is_valid_xml = $doc->schemaValidate('PATH TO XSD');
or else you could use
$is_valid_xml = $doc->schemaValidateSource($source)
This source should be a string containing the schema. It seems that you are using schemaValidateSource function other than schemaValidate. (Once I was stuck in the same place) cheers
Just close your xsd file:
</xsd:schema>
I try it now, and for me this works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With