提问人:Deepak 提问时间:9/15/2023 最后编辑:Deepak 更新时间:9/15/2023 访问量:37
XML XSD 验证 - 国家/地区和州
XML XSD validation - Country and States
问:
我是 XML XSD 验证的新手。我正在尝试验证 XML 中的县作为枚举列表,以及每个国家/地区的州。
我有这个简单的XML:
<?xml version="1.0" encoding="UTF-8"?>
<mylist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<myrow>
<country>Country 1</country>
<state>State 11</state>
</myrow>
<myrow>
<country>Country 2</country>
<state>State 22</state>
</myrow>
<myrow>
<country>Country 3</country>
<state>State 33</state>
</myrow>
</mylist>
我正在尝试验证每个国家/地区的国家/地区和州,如 XSD 所示......
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:element name="mylist">
<xs:complexType>
<xs:sequence>
<xs:element name="myrow" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="country" type="country_type" />
<xs:element name="state" >
<xs:alternative test="$country eq 'Country 1'" type="country_1_states_type"/>
<xs:alternative test="$country eq 'Country 2'" type="country_2_states_type"/>
<xs:alternative test="$country eq 'Country 3'" type="country_3_states_type"/>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- LookUp Lists -->
<xs:simpleType name="country_type">
<xs:restriction base="xs:string">
<xs:enumeration value="Country 1"/>
<xs:enumeration value="Country 2"/>
<xs:enumeration value="Country 3"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="country_1_states_type">
<xs:restriction base="xs:string">
<xs:enumeration value="State 11"/>
<xs:enumeration value="State 12"/>
<xs:enumeration value="State 13"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="country_2_states_type">
<xs:restriction base="xs:string">
<xs:enumeration value="State 21"/>
<xs:enumeration value="State 22"/>
<xs:enumeration value="State 23"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="country_3_states_type">
<xs:restriction base="xs:string">
<xs:enumeration value="State 31"/>
<xs:enumeration value="State 32"/>
<xs:enumeration value="State 33"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
验证者抛出以下错误:A Problem Was Found Starting At: Alternative.
有人可以帮忙吗?
答:
1赞
Michael Kay
9/15/2023
#1
我认为错误消息很可能意味着您的 XSD 处理器仅支持 XSD 1.0 而不是 XSD 1.1(这是必需的)。xs:alternative
但是,您的架构无论如何都不起作用。 只能访问元素的属性,不能访问子元素(也不能访问带符号的变量,这看起来就像你在进行随机猜测)。xs:alternative
$
这更像是 XSD 1.1 断言的用例:
test="(Country='A' and State='B') or (Country='X' and State='Y') or ..."
评论
0赞
Aydin K.
9/15/2023
我还有一本书是你的。正如我们所看到的,XML/XSD 等在 2023 年仍然没有死:)
评论