我正在尝试为类似于以下内容的
XML生成一个非常简单的
XML模式:
- <messages>
- <item>
- <important_tag></important_tag>
- </item>
- <item>
- <important_tag></important_tag>
- <tag2></tag2>
- </item>
- <item>
- <tag2></tag2>
- <tag3></tag3>
- </item>
- </messages>
这个想法是< important_tag>将有一个特定的定义,它可能会或可能不会出现在< item>下.它也可能不止一次出现.
另外,在< important_tag>之前或之后可能存在其他标签.我不能提前说出来的.
我想为< important_tag>提供一个具体的定义.例如,定义它必须包含的属性.
我的意思是,如果important_tag存在,它必须符合我的定义.任何其他标签不必符合任何定义.
我尝试使用以下方案:
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
- <xs:element name="messages">
- <xs:complexType>
- <xs:sequence>
- <xs:element ref="item" maxOccurs="unbounded"/>
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- <xs:element name="item">
- <xs:complexType>
- <xs:sequence>
- <xs:element ref="important_tag" minOccurs="0"/>
- <xs:any minOccurs="0"/>
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- <xs:element name="important_tag">
- <xs:complexType>
- <xs:simpleContent>
- ... specific definitions for important_tag ...
- </xs:simpleContent>
- </xs:complexType>
- </xs:element>
- </xs:schema>
这会导致错误,表明架构不明确.
确切的错误消息是:
- cos-nonambig: '<xs:element ref="important_tag">' makes the content model non-deterministic against '<xs:any>'. Possible causes: name equality,overlapping occurrence or substitution groups.
我正在使用Altova的XML Spy.
我该如何解决这个问题?
谢谢,
达纳
关于错误:该错误消息提到的行不在您包含的xsd中,但其中的这两行是不明确的:
- <xs:element ref="important_tag" minOccurs="0"/>
- <xs:any minOccurs="0"/>
显示歧义的最简单示例是,是否只有一个< important_tag>:
- <important_tag></important_tag>
问题是它可以被解释为一个“important_tag”和零“任何”标签(这是你想要的),但它也可以被解释为零“important_tag”和一个“任何”标签.这是因为“any”标记可以匹配任何标记,包括“important_tag”.
我已经读过,下一版本的XML Schema可以让你说出你的意思:除了important_tag之外的任何标签.
以两种不同方式匹配XML类似于以两种不同方式匹配“a”的正则表达式“a * a *”(一个首先是“a”;或者是一个“a”).这种歧义曾经在DTD的XML规范中被称为“非确定性”,但XML Schema规范将其称为Unique Particle Attribution规则(UPA),这意味着您应该能够分辨出模式的哪个部分获得每个部分的XML文档.