【发布时间】:2011-04-04 18:14:35
【问题描述】:
我有以下 XML:
<?xml version="1.0"?>
<coll xmlns="http://www.example.org/coll" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/coll coll.xsd">
<collection>
<item action="remove">1</item>
<item action="add">2</item>
<item action="add">3</item>
</collection>
<collection>
<item action="add">2</item>
<item action="remove">1</item>
<item action="add">3</item>
</collection>
</coll>
还有一个 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/coll"
xmlns:tns="http://www.example.org/coll" elementFormDefault="qualified">
<element name="coll" type="tns:coll">
</element>
<complexType name="coll">
<sequence>
<element name="collection" type="tns:collection" minOccurs="0" maxOccurs="unbounded"></element>
</sequence>
</complexType>
<complexType name="collection">
<sequence>
<element name="item" type="tns:item" minOccurs="0" maxOccurs="unbounded"></element>
</sequence>
</complexType>
<complexType name="item">
<simpleContent>
<extension base="string">
<attribute name="action">
<simpleType>
<restriction base="string">
<enumeration value="add"></enumeration>
<enumeration value="remove"></enumeration>
</restriction>
</simpleType>
</attribute>
</extension>
</simpleContent>
</complexType>
</schema>
我想限制项目元素的顺序,以便删除总是在添加之前。 IE。第一个集合有效,但第二个无效。
我尝试了两个项目元素,一个是 add_item 类型,一个是 remove_item。但这无法验证 XSD,错误是我不能有两个具有不同类型的同名元素。
有什么想法吗?还是有可能?
【问题讨论】:
-
不在 XML 架构中。你可以使用 schematron。