1

我有一个用例,我需要验证 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。

4

1 回答 1

1

我建议您放弃|分隔符并改用空格 ( )。

IE:

<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>
于 2015-06-23T20:22:19.310 回答