不明确的XML模式

前端之家收集整理的这篇文章主要介绍了不明确的XML模式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为类似于以下内容XML生成一个非常简单的 XML模式:
  1. <messages>
  2. <item>
  3. <important_tag></important_tag>
  4. </item>
  5. <item>
  6. <important_tag></important_tag>
  7. <tag2></tag2>
  8. </item>
  9. <item>
  10. <tag2></tag2>
  11. <tag3></tag3>
  12. </item>
  13. </messages>

这个想法是< important_tag>将有一个特定的定义,它可能会或可能不会出现在< item>下.它也可能不止一次出现.
另外,在< important_tag>之前或之后可能存在其他标签.我不能提前说出来的.

我想为< important_tag>提供一个具体的定义.例如,定义它必须包含的属性.
我的意思是,如果important_tag存在,它必须符合我的定义.任何其他标签不必符合任何定义.

我尝试使用以下方案:

  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  2. <xs:element name="messages">
  3. <xs:complexType>
  4. <xs:sequence>
  5. <xs:element ref="item" maxOccurs="unbounded"/>
  6. </xs:sequence>
  7. </xs:complexType>
  8. </xs:element>
  9. <xs:element name="item">
  10. <xs:complexType>
  11. <xs:sequence>
  12. <xs:element ref="important_tag" minOccurs="0"/>
  13. <xs:any minOccurs="0"/>
  14. </xs:sequence>
  15. </xs:complexType>
  16. </xs:element>
  17. <xs:element name="important_tag">
  18. <xs:complexType>
  19. <xs:simpleContent>
  20. ... specific definitions for important_tag ...
  21. </xs:simpleContent>
  22. </xs:complexType>
  23. </xs:element>
  24. </xs:schema>

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

确切的错误消息是:

  1. 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中,但其中的这两行是不明确的:
  1. <xs:element ref="important_tag" minOccurs="0"/>
  2. <xs:any minOccurs="0"/>

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

  1. <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文档.

猜你在找的XML相关文章