【问题标题】:xml schema check that restriction enumeration value only occrus oncexml 模式检查限制枚举值只出现一次
【发布时间】:2010-11-24 12:26:53
【问题描述】:

我正在创建一个用于验证某些 xml 的 xsd 架构

我想限制xml,所以不能两次输入相同的项目:

<branches>
   <branche>Bank</branche>
   <branche>Bank</branche>
</branches>

但必须可以使用 2 个不同的项目:

<branches>
   <branche>Bank</branche>
   <branche>Insurance</branche>
</branches>

所以我有以下代码:

<!-- definition of simple elements -->
    <xs:simpleType name="branche">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Bank" maxOccurs="1"/>
            <xs:enumeration value="Insurance" maxOccurs="1"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="branches" minOccurs="0"> <!-- minOccurs becouse i want it to be posible to leave out the whole <branches> tag -->
        <xs:complexType>
            <xs:sequence>
                <xs:element name="branche" type="branche" minOccurs="0" maxOccurs="2" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

使用maxOccurs="1" 不会将其限制为只有一个值,因为“分支”标签可以出现两次。

我希望值 (&lt;branche&gt;value&lt;/branche&gt;) 是唯一的。

谢谢!

【问题讨论】:

    标签: xml validation xsd


    【解决方案1】:

    查看身份约束示例here。比如:

    <xs:element name="branches" ...>
      <xs:unique name="...">
        <xs:selector xpath="branche"/>
        <xs:field xpath="."/>
      </xs:key>
    </xs:element>
    

    不太清楚语法,但你明白了。

    【讨论】:

    • 我明白了基本的想法,但它不起作用。我认为“选择器”是错误的,分支必须是 complexType,因为它有子元素。我仍在努力解决这个问题。会及时通知您
    【解决方案2】:

    使用以下代码修复它:

    <xs:element name="branches" minOccurs="0">
       <xs:complexType>
          <xs:sequence>
             <xs:element name="branche" type="branche" minOccurs="0" maxOccurs="2" />
          </xs:sequence>
       </xs:complexType>
       <xs:unique name="brancheUnique">
          <xs:selector xpath="branche"/>
          <xs:field xpath="."/>
       </xs:unique>
    </xs:element>
    

    thnx lexicore 为我指明了正确的方向

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 2015-01-26
      相关资源
      最近更新 更多