我有一个用例,我需要在XSD属性值中验证管道分隔的字符串。
实施例: XML属性
<Fruits Names="Apple|Grapes|Banana">
我想编写一个XSD模式,其中Fruits属性Name允许跟随以及来自上述3个值的其他有效组合。
Apple
Banana
Grapes
Apple|Banana
Grapes|Banana
Apple|Grapes
Banana|Grapes
Grapes|apple
Apple|Grapes|Banana
我目前写了类似
的内容 <xs:simpleType name="Fruits">
<xs:restriction base="xs:string">
<xs:pattern value="Apple*|Grapes*|Banana" ></xs:pattern>
</xs:restriction>
</xs:simpleType>
我想在C#中使用它,所以我想我只能使用XSD 1.0。
答案 0 :(得分:1)
我建议您放弃|
分隔符并使用空格()。
即:
<Fruits Names="Apple Grapes Banana"/>
然后,以下XSD将满足您的要求:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Fruits">
<xs:complexType>
<xs:attribute name="Names">
<xs:simpleType>
<xs:list itemType="FruitTypes"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:simpleType name="FruitTypes">
<xs:restriction base="xs:string">
<xs:enumeration value="Apple"/>
<xs:enumeration value="Grapes"/>
<xs:enumeration value="BAnana"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>