当前位置: 首页 > 网络学院 > XML相关教程 > Schema (XSD) > XSD 属性

Schema (XSD)
Schema (XSD) 介绍
为何使用 XML Schemas?
如何定制 XSD
XSD - <schema>元素
XSD 简单元素
XSD 属性
XSD 约束面
XSD 复合元素
XSD 复合空元素
XSD 复合纯元素
XSD 复合纯文本
XSD 混合内容的复合类型
XSD 指示器复合类型
XSD <any> 元素
XSD <anyAttribute> 元素
XSD 元素替代
XSD 实例
XSD 字符串数据类型
XSD 日期数据类型
XSD 小数数据类型

Schema (XSD) 中的 XSD 属性


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 897 ::
收藏到网摘: n/a

All attributes are declared as simple types.
所有的属性都是以简单类型来声明的。


What is an Attribute?
属性是什么?

Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type.
简单元素不包含属性。当元素包含属性时,它会被判断成复合元素。但属性本身总是以简单类型来声明的。


How to Define an Attribute?
如何定义属性?

The syntax for defining an attribute is:
定义一个属性的语法如下:

<xs:attribute name="xxx" type="yyy"/>

where xxx is the name of the attribute and yyy specifies the data type of the attribute.
上述案例中,xxx是属性名称,yyy指定了属性的数据类型。

XML Schema has a lot of built-in data types. The most common types are:
XML Schema本身包含多种数据类型。最常见的数据类型如下:

  • xs:string
    xs:字符串
  • xs:decimal
    xs:小数
  • xs:integer
    xs:整数
  • xs:boolean
    xs:逻辑值
  • xs:date
    xs:日期
  • xs:time
    xs:时间

Example
案例

Here is an XML element with an attribute:
下面列举了一个包含属性的XML元素:

<lastname lang="EN">Smith</lastname>

And here is the corresponding attribute definition:
下面列举了相应的属性定义:

<xs:attribute name="lang" type="xs:string"/>

Default and Fixed Values for Attributes
属性的默认值和固定值

Attributes may have a default value OR a fixed value specified.
属性可以包含指定的默认值或固定值。

A default value is automatically assigned to the attribute when no other value is specified.
当未指定其它值时,属性会自动设置默认值。

In the following example the default value is "EN":
下述案例中的默认值是"EN":

<xs:attribute name="lang" type="xs:string" default="EN"/>

A fixed value is also automatically assigned to the attribute, and you cannot specify another value.
固定值是也是系统自动分派给属性的。并且,一旦指定了固定值,你就不能指定其它值了。

In the following example the fixed value is "EN":
下述案例中的固定值是"EN":

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

Optional and Required Attributes
可选属性和必要属性

Attributes are optional by default. To specify that the attribute is required, use the "use" attribute:
属性是默认为可选的。要指明属性是必要的,就必须使用“use”属性:

<xs:attribute name="lang" type="xs:string" use="required"/>

Restrictions on Content
对内容的约束

When an XML element or attribute has a data type defined, it puts restrictions on the element's or attribute's content.
当XML元素或属性包含了已定义的数据类型,元素或属性的内容就会受到约束。

If an XML element is of type "xs:date" and contains a string like "Hello World", the element will not validate.
如果一个XML元素是"xs:date"类型,并包含一个诸如"Hello World"这样的字符串,那么该元素将失效。

With XML Schemas, you can also add your own restrictions to your XML elements and attributes. These restrictions are called facets. You can read more about facets in the next chapter.
你也可以使用XML Schema给XML元素和属性添加约束限制。这些约束称为“约束面(facet)”。在下面一章里,你可以学习更多关于“约束面”的内容。

评论 (0) All

登陆 | 还没注册?