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”
- baz 只能是“boom”或“box”
我想将此列表放入 XSD 中,因此 XML 作者无法输入未定义的键或该键不允许的值,
我知道最好去掉键属性,并制作类似
<List>
<foo>bar</foo>
<baz>boom</foo>
</List>
的东西,但不幸的是,格式必须保持不变,但有更多限制。我必须根据情况限制内容键属性中给出的内容。
我读了
这里第二个答案中提到了
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
In XML Schema 1.1, xs:alternative can specify attribute-dependent types, as shown at your link (
就您的示例而言,架构可能看起来如下:
有效的XML……
被评估为有效。
由于重复的键而引起的无效XML………
被评估为消息无效:
由于值无效的XML…………
被评估为无效的消息:
用在线架构验证器服务(链接为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:
A valid xml…
…is assessed as valid.
An invalid xml due to duplicate keys…
…is assessed as invalid with messages:
An invalid xml due to invalid values…
…is assessed as invalid with messages:
Tested with Online Schema Validator service (linked from Xerces Using XML Schemas) running "Apache Xerces-J (v 2.12.2) XML Schema validator".