不明确的XML模式

Dan*_*ana 7 xml xsd ambiguity

我正在尝试为类似于以下内容的XML生成一个非常简单的XML模式:

<messages>
  <item>
    <important_tag></important_tag>
  </item>
  <item>
    <important_tag></important_tag>
    <tag2></tag2>
  </item>
  <item>
    <tag2></tag2>
    <tag3></tag3>
  </item>
</messages>
Run Code Online (Sandbox Code Playgroud)

这个想法是<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>
Run Code Online (Sandbox Code Playgroud)

这会导致错误,表明架构不明确.

确切的错误消息是:

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.
Run Code Online (Sandbox Code Playgroud)

我正在使用Altova的XML Spy.

我该如何解决这个问题?

谢谢,达娜

Yos*_*han 7

在MSDN上有一篇很棒的文章讨论了设计可扩展模式的问题,你可以在这里找到,我建议你仔细研究一下,但具体到你的观点,它解释了为什么你在第2点的"使用XML"中得到这个错误的原因.用于设计可版本化XML格式的架构"(您可以搜索"非确定性"并直接在那里.

基本上,一旦你有一个xs:any元素,验证器就不能假设其他兄弟元素,所以 - 你可能有一个不需要那些强制属性的important_tag的定义,因此这些元素无法验证


13r*_*ren 6

关于错误:该错误消息提到的行不在您包含的xsd中,但其中的这两行是不明确的:

<xs:element ref="important_tag" minOccurs="0"/>
<xs:any minOccurs="0"/>
Run Code Online (Sandbox Code Playgroud)

显示歧义的最简单的例子是,如果只有一个<important_tag>:

  <important_tag></important_tag>
Run Code Online (Sandbox Code Playgroud)

问题是它可以被解释为一个"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文档.