PHP libxml中针对XSD的XML验证

时间:2022-12-30 17:19:31

I have created an xml like below

我创建了一个像下面的xml

<Request>
    <RequestType>Logon</RequestType>
    <MobileId>23424</MobileId>
    <Password>123456Gg</Password>
</Request>

and my xsd file is like below code

我的xsd文件就像下面的代码

<?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

我使用PHP的DOMDocument的schemaValidate函数来验证xml对xsd,并给出以下错误

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.

但是我已在此链接W3C XML Schema Online验证中测试了这两个文件(xml和xsd),并且它成功验证后没有显示任何错误。

What I have to do to get work this in php?

我需要做些什么才能在php中完成这项工作?

Note: my php libxml version is 2.7.8

注意:我的php libxml版本是2.7.8

2 个解决方案

#1


11  

dom specially gives two functions to validate with schema. One is to give file path

dom特别提供了两个函数来验证模式。一个是给文件路径

$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

此源应该是包含架构的字符串。您似乎使用schemaValidate以外的schemaValidateSource函数。 (一旦我被困在同一个地方)欢呼

#2


1  

Just close your xsd file:

只需关闭您的xsd文件:

</xsd:schema>

I try it now, and for me this works.

我现在试试,对我来说这很有用。

#1


11  

dom specially gives two functions to validate with schema. One is to give file path

dom特别提供了两个函数来验证模式。一个是给文件路径

$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

此源应该是包含架构的字符串。您似乎使用schemaValidate以外的schemaValidateSource函数。 (一旦我被困在同一个地方)欢呼

#2


1  

Just close your xsd file:

只需关闭您的xsd文件:

</xsd:schema>

I try it now, and for me this works.

我现在试试,对我来说这很有用。