XSD 根据属性值限制元素的内容

发布于 2025-01-19 16:29:54 字数 1873 浏览 0 评论 0原文

形式上,我们的 XSD 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="fooxsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

  <xs:element name="List">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Config" type="ConfigType" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="Entry">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="key" type="xs:ID" use="required" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

</xs:schema>

XML 可以看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
  <Entry key="foo">bar</Config>
  <Entry key="baz">boom</Config>
</List>
  • 所有条目都是可选的
  • 所有键都必须是唯一的
  • 键的顺序是不相关的

除了键值列表之外什么都没有。

现在我有一个这样的约束列表:

  • foo 只能是“bar2 或“bar2”
  • baz 只能是“boom”或“box”

我想将此列表放入 XSD 中,因此 XML 作者无法输入未定义的键或该键不允许的值,

我知道最好去掉键属性,并制作类似

<List>
  <foo>bar</foo>
  <baz>boom</foo>
</List>

的东西,但不幸的是,格式必须保持不变,但有更多限制。我必须根据情况限制内容键属性中给出的内容。

我读了

这里第二个答案中提到了元素 - 这可能是一个丑陋的解决方案,但也许存在。使用通常的 xsd-elements 是更好的吗?

Formally our XSD looked like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="fooxsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

  <xs:element name="List">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Config" type="ConfigType" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="Entry">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="key" type="xs:ID" use="required" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

</xs:schema>

XMLs can look like this:

<?xml version="1.0" encoding="utf-8"?>
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
  <Entry key="foo">bar</Config>
  <Entry key="baz">boom</Config>
</List>
  • all entries are optional
  • all keys have to be unique
  • order of keys are irrelevant

Nothing but a key-value list.

Now I have a list of constraints like this:

  • foo can only be "bar2 or "bar2"
  • baz can only be "boom" or "box"

I want to put this list into the XSD, so XML-authors can not enter undefined keys or values which are not allowed for that key.

I know it would be better to get rid of the key-attribute, and make something like

<List>
  <foo>bar</foo>
  <baz>boom</foo>
</List>

But unfortunately I can not; the format must remain the same. But with more restrictions. I have to restrict the content depending to what's given in the key-attribute.

I read

Here the <xs:assert>-element is mentioned in the second answer - that could be a solution. An ugly one though, but maybe there is better one using the usual xsd-elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2025-01-26 16:29:54

In XML Schema 1.1, xs:alternative can specify attribute-dependent types, as shown at your link (

就您的示例而言,架构可能看起来如下:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:xsv="http://www.w3.org/2007/XMLSchema-versioning"
           elementFormDefault="qualified"
           xsv:minVersion="1.1">

  <xs:element name="List">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Config" minOccurs="0" maxOccurs="unbounded">
          <xs:alternative test="@key='foo'" type="ConfigKeyFooType"/>
          <xs:alternative test="@key='baz'" type="ConfigKeyBazType"/>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:attributeGroup name="ConfigTypeAttributeGroup">
    <xs:attribute name="key" type="xs:ID" use="required" />
  </xs:attributeGroup>

  <xs:complexType name="ConfigKeyFooType">
    <xs:simpleContent>
      <xs:extension base="ConfigKeyFooContentType">
        <xs:attributeGroup ref="ConfigTypeAttributeGroup"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:simpleType name="ConfigKeyFooContentType">
    <xs:restriction base="xs:NMTOKEN">
      <xs:enumeration value="bar"/>
      <xs:enumeration value="bar2"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="ConfigKeyBazType">
    <xs:simpleContent>
      <xs:extension base="ConfigKeyBazContentType">
        <xs:attributeGroup ref="ConfigTypeAttributeGroup"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:simpleType name="ConfigKeyBazContentType">
    <xs:restriction base="xs:NMTOKEN">
      <xs:enumeration value="boom"/>
      <xs:enumeration value="box"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

有效的XML……

<?xml version="1.0" encoding="utf-8"?>
<List>
  <Config key="foo">bar</Config>
  <Config key="baz">boom</Config>
</List>

被评估为有效。

由于重复的键而引起的无效XML………

<?xml version="1.0" encoding="utf-8"?>
<List>
  <Config key="foo">bar</Config>
  <Config key="baz">boom</Config>
  <Config key="foo">bar2</Config>
  <Config key="baz">box</Config>
</List>

被评估为消息无效:

[Error] sample-bad-dup-keys.xml:5:21:cvc-id.2: There are multiple occurrences of ID value 'foo'.
[Error] sample-bad-dup-keys.xml:5:21:cvc-attribute.3: The value 'foo' of attribute 'key' on element 'Config' is not valid with respect to its type, 'ID'.
[Error] sample-bad-dup-keys.xml:6:21:cvc-id.2: There are multiple occurrences of ID value 'baz'.
[Error] sample-bad-dup-keys.xml:6:21:cvc-attribute.3: The value 'baz' of attribute 'key' on element 'Config' is not valid with respect to its type, 'ID'.

由于值无效的XML…………

<?xml version="1.0" encoding="utf-8"?>
<List>
  <Config key="foo">bar3</Config>
  <Config key="baz">boox</Config>
</List>

被评估为无效的消息:

[Error] sample-bad-values.xml:3:34:cvc-enumeration-valid: Value 'bar3' is not facet-valid with respect to enumeration '[bar, bar2]'. It must be a value from the enumeration.
[Error] sample-bad-values.xml:3:34:cvc-complex-type.2.2: Element 'Config' must have no element [children], and the value must be valid.
[Error] sample-bad-values.xml:4:34:cvc-enumeration-valid: Value 'boox' is not facet-valid with respect to enumeration '[boom, box]'. It must be a value from the enumeration.
[Error] sample-bad-values.xml:4:34:cvc-complex-type.2.2: Element 'Config' must have no element [children], and the value must be valid.

在线架构验证器服务(链接为Xerces 使用xml schemas )运行“ apache xerces-j(v 2.12.2)xml schema验证器”。

In XML Schema 1.1, xs:alternative can specify attribute-dependent types, as shown at your link (Restrict XSD attribute value based on another attribute value).

For your example, a schema might look like the following:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:xsv="http://www.w3.org/2007/XMLSchema-versioning"
           elementFormDefault="qualified"
           xsv:minVersion="1.1">

  <xs:element name="List">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Config" minOccurs="0" maxOccurs="unbounded">
          <xs:alternative test="@key='foo'" type="ConfigKeyFooType"/>
          <xs:alternative test="@key='baz'" type="ConfigKeyBazType"/>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:attributeGroup name="ConfigTypeAttributeGroup">
    <xs:attribute name="key" type="xs:ID" use="required" />
  </xs:attributeGroup>

  <xs:complexType name="ConfigKeyFooType">
    <xs:simpleContent>
      <xs:extension base="ConfigKeyFooContentType">
        <xs:attributeGroup ref="ConfigTypeAttributeGroup"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:simpleType name="ConfigKeyFooContentType">
    <xs:restriction base="xs:NMTOKEN">
      <xs:enumeration value="bar"/>
      <xs:enumeration value="bar2"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="ConfigKeyBazType">
    <xs:simpleContent>
      <xs:extension base="ConfigKeyBazContentType">
        <xs:attributeGroup ref="ConfigTypeAttributeGroup"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:simpleType name="ConfigKeyBazContentType">
    <xs:restriction base="xs:NMTOKEN">
      <xs:enumeration value="boom"/>
      <xs:enumeration value="box"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

A valid xml…

<?xml version="1.0" encoding="utf-8"?>
<List>
  <Config key="foo">bar</Config>
  <Config key="baz">boom</Config>
</List>

…is assessed as valid.

An invalid xml due to duplicate keys…

<?xml version="1.0" encoding="utf-8"?>
<List>
  <Config key="foo">bar</Config>
  <Config key="baz">boom</Config>
  <Config key="foo">bar2</Config>
  <Config key="baz">box</Config>
</List>

…is assessed as invalid with messages:

[Error] sample-bad-dup-keys.xml:5:21:cvc-id.2: There are multiple occurrences of ID value 'foo'.
[Error] sample-bad-dup-keys.xml:5:21:cvc-attribute.3: The value 'foo' of attribute 'key' on element 'Config' is not valid with respect to its type, 'ID'.
[Error] sample-bad-dup-keys.xml:6:21:cvc-id.2: There are multiple occurrences of ID value 'baz'.
[Error] sample-bad-dup-keys.xml:6:21:cvc-attribute.3: The value 'baz' of attribute 'key' on element 'Config' is not valid with respect to its type, 'ID'.

An invalid xml due to invalid values…

<?xml version="1.0" encoding="utf-8"?>
<List>
  <Config key="foo">bar3</Config>
  <Config key="baz">boox</Config>
</List>

…is assessed as invalid with messages:

[Error] sample-bad-values.xml:3:34:cvc-enumeration-valid: Value 'bar3' is not facet-valid with respect to enumeration '[bar, bar2]'. It must be a value from the enumeration.
[Error] sample-bad-values.xml:3:34:cvc-complex-type.2.2: Element 'Config' must have no element [children], and the value must be valid.
[Error] sample-bad-values.xml:4:34:cvc-enumeration-valid: Value 'boox' is not facet-valid with respect to enumeration '[boom, box]'. It must be a value from the enumeration.
[Error] sample-bad-values.xml:4:34:cvc-complex-type.2.2: Element 'Config' must have no element [children], and the value must be valid.

Tested with Online Schema Validator service (linked from Xerces Using XML Schemas) running "Apache Xerces-J (v 2.12.2) XML Schema validator".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文