XSD:元素和属性之间的区别 [英] XSD: difference between Element and Attribute

查看:22
本文介绍了XSD:元素和属性之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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屋!

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