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

  • 本文关键字:元素 属性 XSD xml xsd
  • 更新时间 :
  • 英文 :


我们的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";bar2">
  • baz只能是";繁荣;或";框">

我想将此列表放入XSD中,这样XML作者就不能输入未定义的键或该键不允许的值。

我知道最好去掉关键属性,做一些类似的东西

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

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

我读

  • 基于另一个属性值限制XSD属性值

这里是<xs:assert&gt-第二个答案中提到了元素,这可能是一个解决方案。虽然这是一个丑陋的例子,但也许有更好的例子使用常见的xsd元素?

在XMLSchema1.1中,xs:alternative可以指定依赖于属性的类型,如链接中所示(根据另一个属性值限制XSD属性值(。

对于您的示例,模式可能如下所示:

<?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.

用运行"的在线模式验证器服务(使用XML模式从Xerces链接(进行测试;Apache Xerces-J(2.12.2版(XML模式验证器";。

最新更新