不明确的 XML 模式 [英] Ambiguous XML schema

查看:36
本文介绍了不明确的 XML 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to produce a pretty simple XML schema for an XML similar to the following:

<messages>
  <item>
    <important_tag></important_tag>
  </item>
  <item>
    <important_tag></important_tag>
    <tag2></tag2>
  </item>
  <item>
    <tag2></tag2>
    <tag3></tag3>
  </item>
</messages>

这个想法是 将有一个特定的定义,它可能出现也可能不会出现在 下.它也可能出现不止一次.此外,在 之前或之后可能还有其他我无法提前命名的标签.

The idea is that <important_tag> will have a specific definition AND it may or may not appear under <item>. It may also appear more than once. Additionally, there may be other tags before or after <important_tag> that I can not name in advance.

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

I would like to give a specific definition for <important_tag>. For example, define attributes that it must contain. What I mean is that if important_tag is present it must conform to my definition. Any other tag doesn't have to conform to any definition.

我尝试使用以下方案:

<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>

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

This results in an error saying that the schema is ambiguous.

确切的错误信息是:

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.

I'm using Altova's XML Spy.

我该如何解决这个问题?

How do I solve this?

谢谢,达纳

推荐答案

关于错误:该错误消息提到了一行不在您包含的 xsd 中,但其中的这两行有歧义:

Regarding the error: that error message mentions a line that's not in the xsd you included, but these two lines in it are ambiguous:

<xs:element ref="important_tag" minOccurs="0"/>
<xs:any minOccurs="0"/>

显示歧义的最简单示例是如果只有一个 :

The simplest example to show the ambiguity is if there was just one <important_tag>:

  <important_tag></important_tag>

问题是它可以被解释为一个important_tag"和零个any"标签(这是你想要的),但它也可以解释为零个important_tag"和一个any"标签.这是因为any"标签可以匹配任何标签,包括important_tag".

The problem is that it could be interpreted as one "important_tag" and zero "any" tags (which is what you wanted), but it can also be interpreted as zero "important_tag" and one "any" tags. This is because the "any" tag can match any tag, including "important_tag".

我已经读到下一版本的 XML Schema 使您能够说出您的意思:任何标签除了important_tag.

I've read that the next version of XML Schema enables you to say what you meant: any tag except important_tag.

以两种不同的方式匹配 XML 类似于正则表达式a*a*"以两种不同的方式匹配a"(一个是第一个a";一个第二个是a").这种歧义在 DTD 的 XML 规范中曾经被称为非确定性",但 XML 架构规范将其称为唯一粒子属性规则 (UPA),这意味着您应该能够分辨架构的哪个部分获得每个部分XML 文档.

Matching the XML in two different ways is similar to the regular expression "a*a*" matching "a" in two different ways (one first "a"; or one second "a"). This ambiguity used to be called "non-deterministic" in the XML spec for DTDs, but the XML Schema spec calls it the Unique Particle Attribution rule (UPA), meaning that you should be able to tell which part of the schema gets each part of the XML document.

这篇关于不明确的 XML 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆