XSD:元素和属性的区别 [英] XSD: difference between Element and Attribute
问题描述
我是 XSD 新手,不知道什么时候用属性,什么时候用元素?
I'm new to XSD, and I'm quite confused as to when to use attribute, and when to use element?
为什么我们不能在属性中指定 minOccurs 和 maxOccurs?
Why cant we specify minOccurs and maxOccurs in attribute?
另外,为什么我们不能在元素中指定 use="required"?
Also, why is it we cannot specify use="required" in element?
推荐答案
一个元素是一个 XML 元素——一个开始标签、一些内容、一个结束标签——它们是你的 XML 文档的构建块:
An element is an XML element - a opening tag, some content, a closing tag - they are the building blocks of your XML document:
<test>someValue</test>
这里,test"将是一个元素.
Here, "test" would be an element.
属性是标签的附加信息——它是一个附加"或元素的额外信息,但永远不会单独存在:
Attributes is an additional info on a tag - it's an "add-on" or an extra info on an element, but can never exist alone:
<test id="5">somevalue</test>
id"是一个属性.
一个标签上不能有多个同名属性 --> minOccurs/maxOccurs 没有意义.您可以为属性定义必需(或不必需) - 其他任何内容都没有意义.
You cannot have multiple attributes of the same name on a single tag --> minOccurs/maxOccurs makes no sense. You can define required (or not) for an attribute - anything else doesn't make sense.
元素由它们在复杂类型中的出现来定义 - 例如如果您有一个复杂类型,其中包含 <xs:sequence>
- 您正在定义所有元素都必须存在并且必须按以下特定顺序:
The elements are defined by their occurrence inside complex types - e.g. if you have a complex type with a <xs:sequence>
inside - you are defining that all elements must be present and must the in this particular order:
<xs:complexType name="SomeType">
<xs:sequence>
<xs:element name="Element1" type="xs:string" />
<xs:element name="Element2" type="xs:string" />
</xs:sequence>
</xs:complexType>
在该类型的元素中,子元素Element1"和Element2"是必需的,并且必须按此顺序出现 - 不需要必需"或不需要"(与属性一样).是否需要一个元素是通过使用 minOccurs 和 maxOccurs 来定义的;默认情况下两者都是 =1,例如元素必须出现,并且只能出现一次.通过调整这些设置,您可以将元素定义为可选元素 (minOccurs=0),或允许其多次出现 (maxOccurs > 1).
Inside an element of that type, the sub-elements "Element1" and "Element2" are required and must appear in this order - there's no need for "required" or not (like with attributes). Whether or not an element is required is defined by the use of minOccurs and maxOccurs; both are =1 by default, e.g. the element must occur, and can only occur once. By tweaking those settings, you can define an element to be optional (minOccurs=0), or allow it to show up several times (maxOccurs > 1).
我强烈建议您查看W3Schools XML Schema 教程 并学习有关 XML 架构的更多信息.
I'd strongly recommend you check out the W3Schools Tutorial on XML Schema and learn some more about XML schema.
马克
这篇关于XSD:元素和属性的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!