xml xsd:any 和 xsd:anyType 之间的差异/相似之处

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5389076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:37:02  来源:igfitidea点击:

Difference/similarities between xsd:any and xsd:anyType

xmlxsd

提问by user989812323

I am reading about XML, XML-Schema, DTD and I don't really understand the difference between xsd:anyand xsd:anyType.

我正在阅读有关 XML、XML-Schema、DTD 的内容,但我并不真正理解xsd:anyxsd:anyType之间的区别。

Can someone explain this to me or point to some good article? (please don't link to the XML-Schema specifications - I read that and I'm more confused)

有人可以向我解释这一点或指出一些好文章吗?(请不要链接到 XML-Schema 规范 - 我读过,但我更困惑)

TIA

TIA

回答by dogbane

This postexplains it nicely. I quote:

这篇文章很好地解释了它。我引用:

xsd:anyType is a type, like xsd:integer (though xsd:anyType is special in that it can act as a simple or complex type, and it places essentially no restrictions on the tree that it validates -- think of it loosely as the Schema language's analog of java.lang.Object).

A sample use would be:

xsd:anyType 是一种类型,就像 xsd:integer(尽管 xsd:anyType 的特殊之处在于它可以充当简单或复杂类型,并且它基本上对它验证的树没有任何限制——可以将它松散地视为模式语言类似于 java.lang.Object)。

示例用途是:

<xsd:element name="e" type="xsd:anyType"/>

This would mean that elements named <e>can have any content, any attributes, etc.

xs:any is a wildcard, usable as a term in a content model. For example:

这意味着命名的元素 <e>可以具有任何内容、任何属性等。

xs:any 是通配符,可用作内容模型中的术语。例如:

<xsd:complexType name="T">
  <xsd:sequence>
    <xsd:element ref="A"/>
    <xsd:any />
    <xsd:element ref="C"/>
  </xsd:sequence>
</xsd:complexType>

Elements of type T must have content <A/><???/><C/>, where <???>can be any named element. Now, if you look really closely there is an approximation to the definition of xsd:anyType given for reference in the Recommendation, and it uses an xsd:any wildcard as the means of saying that it allows any elements.

类型 T 的元素必须具有 content <A/><???/><C/>,其中<???>可以是任何命名元素。现在,如果您仔细观察,就会发现建议书中给出的用于参考的 xsd:anyType 的近似定义,它使用 xsd:any 通配符来表示它允许任何元素。

Also take a look at the XML Schema.

另请查看XML 架构

回答by Benjamin Horstman

The mailing list post linked in dogbane's answer wasn't clear to me until I created the following example:

在创建以下示例之前,我不清楚在 dogbane 的回答中链接的邮件列表帖子:

With anyType schema:

使用 anyType 架构:

<xsd:complexType name="Outer">
    <xsd:element name="e" type="xsd:anyType" />
</xsd:complexType>

Which allows this format:

这允许这种格式:

<Outer>
    <e> // must be called "e"
        // but anything can go inside
    </e>
</Outer>

And with any schema:

并使用任何架构:

<xsd:complexType name="Outer">
    <xsd:any />
</xsd:complexType>

Which allows this format:

这允许这种格式:

<Outer>
    //anything can go inside
</Outer>

So anyType is a type, and any is an element

所以 anyType 是一个类型,而 any 是一个元素