【发布时间】:2016-01-27 08:08:52
【问题描述】:
我正在尝试使用 xsd 文件验证 XML 文件。我使用
链接 XML 和 XSDschemaLocation
但 XML 不读取 XSD 文件。
XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="one.xsd">
<person>
<full_name>Hege Refsnes</full_name>
<child_name>Cecilie</child_name>
</person>
<person>
<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>
</person>
<person>
<full_name>Stale Refsnes</full_name>
</person>
</persons>
XSD 文件
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
minOccurs="1" maxOccurs="2"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
如果 XML 检测到这个 XSD 文件,它应该返回一个错误,(因为我使用了“name”而不是“full_name”,所以我也限制了“child_name”的 min - 1 和 max - 2)
谁能解释一下如何链接 XML 和 XSD?
【问题讨论】:
-
你是用什么来读取XML文档的,能否提供源代码?
标签: xml xsd xsd-validation