9

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

Kanagu
  • 606
  • 3
  • 9
  • 17

2 Answers2

12

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

User123456
  • 2,492
  • 3
  • 30
  • 44
1

Just close your xsd file:

</xsd:schema>

I try it now, and for me this works.

Rafael Soufraz
  • 974
  • 3
  • 11
  • 28
  • Sorry I have used valid xsd which it validates the xml in the mentioned url ([xml schema validation](http://www.utilities-online.info/xsdvalidation/#.UsqYpNIW2Fk)) Here I have missed to include – Kanagu Jan 06 '14 at 13:23
  • 2
    I got it to work, what I am doing wrong here is, I have used $xml = simplexml_load_string(xmlstring); then used that xml object into DOMDocument::loadXML($xml), when i used xmlstring directly into DOMDocument then it is working fine – Kanagu Jan 06 '14 at 13:46
  • Vote up for you for answering your own question. :p – Rafael Soufraz Jan 06 '14 at 13:56
  • Hey Kangu, What else did you do to fix this? I'm really struggling here. Like you described, if I hard code the xml into the loadXML method it works. If I try to post xml to the php page, I get.... "The document has no document element." – tree May 13 '14 at 22:41