根据其他 XML 字段的值在 XSD 中设置 minOccurs 和 maxOccurs? [英] Setting minOccurs and maxOccurs in XSD based on value of other XML field?

查看:40
本文介绍了根据其他 XML 字段的值在 XSD 中设置 minOccurs 和 maxOccurs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XSD 来验证 XML 文件.结构如下:

I have an XSD to validate an XML file. The structure is as follows:

<root>
    <child>
        <size>2</size>
        <childElement>Element 1</childElement>
        <childElement>Element 2</childElement>
    </child>
</root>

childElement 的数量取决于提供的大小,即如果 size 设置为 3,则不超过 3 个 childElement 可以添加.

The number of childElements is dependent on the size provided i.e. if size was set as 3, not more than 3 childElements can be added.

我尝试过使用 xs:alternative 但它似乎不起作用:

I have tried using xs:alternative but it does not seem to work:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="child" minOccurs="1" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="size" type="xs:integer" maxOccurs="1"/>
                            <xs:element name="childElement" type="xs:string" maxOccurs="1">
                                <xs:alternative test="@size>1" maxOccurs="@size"/>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

有没有办法使用 xs:alternative 或其他标签来实现这一点,或者这超出了 XSD 的可能性范围?

Is there a way of using xs:alternative or another tag to achieve this, or is this outside the realm of possibility with XSD?

推荐答案

设计建议:如果您的 XML 设计仍然可以更改,请消除 size 元素并传达隐式信息而不是显式信息.通过消除信息重复,您将无需检查重复是否一致.

Design recommendation: If your XML design can still be changed, eliminate the size element and convey that information implicitly rather than explicitly. By eliminating the duplication of information, you'll not need to check that the duplication is consistent.

如果您的 XML 设计仍然无法更改,或者您选择不更改它...

If your XML design cannot still be changed, or if you choose not to change it...

不可能.必须通过 XSD 进行带外检查.

Not possible. Would have to be checked out-of-band wrt XSD.

可以使用 xs:assert:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="child">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="size" type="xs:integer"/>
              <xs:element name="childElement" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:assert test="count(childElement) = size"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

这篇关于根据其他 XML 字段的值在 XSD 中设置 minOccurs 和 maxOccurs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆