这是一个简单的 XSD+XML 示例。如何使验证工作?
Here is a simple XSD+XML example. How to make the validation work?
我有一个 XSD 有两个定义的类型和一个简单的 XML 来验证。错误信息是
src-resolve: Cannot resolve the name 'colorType' to a(n) 'type definition' component
那个错误消息有几个问题,但情况似乎是其他的。此外,示例文档通常很大,我在查看相关部分时遇到问题。所以这是一个简单的。
XSD(命名为 svg_export_test.xsd
):
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="test"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="colorType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="#[0-9A-Fa-f]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="clothingType">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="color" type="colorType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML(在同一文件夹中命名为svg_export_test.xml
):
<?xml version="1.0"?>
<t:clothingType xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="svg_export_test.xsd"
xmlns:t="test"
>
<t:name>Shirt</t:name>
<t:color>#00000000</t:color>
</t:clothingType>
首先,您说的是 type="colorType"
,但 colorType
不是类型,而是元素声明。您需要在架构的顶层有一个 <xs:simpleType name="colorType">
才能正常工作。
其次,如果你添加这个,类型colorType
将在命名空间test
中,所以要引用它,你需要使用type="t:colorType"
,其中命名空间前缀t
绑定到命名空间 test
(将 xmlns:t="test"
添加到您的 xs:schema
元素)
更正后的 XSD 将是
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="test" xmlns:t="test"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:simpleType name="colorType">
<xs:restriction base="xs:string">
<xs:pattern value="#[0-9A-Fa-f]{8}"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="clothingType">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="color" type="t:colorType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我有一个 XSD 有两个定义的类型和一个简单的 XML 来验证。错误信息是
src-resolve: Cannot resolve the name 'colorType' to a(n) 'type definition' component
那个错误消息有几个问题,但情况似乎是其他的。此外,示例文档通常很大,我在查看相关部分时遇到问题。所以这是一个简单的。
XSD(命名为 svg_export_test.xsd
):
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="test"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="colorType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="#[0-9A-Fa-f]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="clothingType">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="color" type="colorType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML(在同一文件夹中命名为svg_export_test.xml
):
<?xml version="1.0"?>
<t:clothingType xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="svg_export_test.xsd"
xmlns:t="test"
>
<t:name>Shirt</t:name>
<t:color>#00000000</t:color>
</t:clothingType>
首先,您说的是 type="colorType"
,但 colorType
不是类型,而是元素声明。您需要在架构的顶层有一个 <xs:simpleType name="colorType">
才能正常工作。
其次,如果你添加这个,类型colorType
将在命名空间test
中,所以要引用它,你需要使用type="t:colorType"
,其中命名空间前缀t
绑定到命名空间 test
(将 xmlns:t="test"
添加到您的 xs:schema
元素)
更正后的 XSD 将是
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="test" xmlns:t="test"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:simpleType name="colorType">
<xs:restriction base="xs:string">
<xs:pattern value="#[0-9A-Fa-f]{8}"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="clothingType">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="color" type="t:colorType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>