xml XSD 中元素的 ref 属性有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1448888/
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
What does the ref attribute on an element in an XSD do?
提问by Fiona - myaccessible.website
Documentation seems to say that it references another element in the schema, but how could it be used - I've never seen it in schemas that I have worked with.
文档似乎说它引用了模式中的另一个元素,但是如何使用它 - 我从未在我使用过的模式中看到它。
Anyone got any nice use cases or something that could explain its use further?
任何人都有任何很好的用例或可以进一步解释其用途的东西?
回答by grkvlt
Basically it references another element that is delared elsewhere, which may or may not be the same schema document. For instance, it could come from an externally referenced schema in a different namespace. Suppose you use the itemelement a lot in several different schemas, you can declare it (and any other common types and attributes) in a commonschema, and then reuse those in all your other schemas. If you reference your commonschema with the namespace c, you can declare an instance of the itemelemnt on its own or as part of a type as follows:
基本上它引用了在别处声明的另一个元素,它可能是也可能不是相同的模式文档。例如,它可能来自不同命名空间中的外部引用模式。假设您在几个不同的模式中多次使用item元素,您可以在一个公共模式中声明它(以及任何其他常见类型和属性),然后在所有其他模式中重用它们。如果您使用命名空间c引用您的公共架构,您可以单独声明item元素的实例或作为类型的一部分,如下所示:
<xs:element ref="c:item" /><!-- reference here -->
<xs:complexType name="something">
<xs:sequence>
<xs:element ref="c:item" /><!-- and here -->
</xs:sequence>
<xs:element name="other" type="xs:Name" />
</xs:complexType>
The definition in the data schema would look like this:
数据模式中的定义如下所示:
<xs:element name="item" type="itemType" /><!-- referenced element -->
<xs:complexType name="itemType">
<xs:sequence>
<xs:element name="code" type="xs:Name" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="description" type="xs:normalizedString" use="required" />
</xs:complexType>
回答by zedoo
For example if you want to declare element types that can appear deeply nested, but also as top level elements in an instance document.
例如,如果您想声明可以出现深度嵌套的元素类型,但也可以作为实例文档中的顶级元素。
The XML Schema Primer has examples for this: http://www.w3.org/TR/xmlschema-0/
XML Schema Primer 有这方面的例子:http: //www.w3.org/TR/xmlschema-0/

