XSD - 属性
属性表示 XML 元素的属性。XSD 将其定义为一种简单类型。
语法
<xs:attribute name = "attribute-name" type = "attribute-type"/>
| attribute-name | 属性的名称。例如,<xs:attribute name = "rollno" type = "xs:integer"/> 将定义以下 rollno 属性,该属性可在 XML 元素中使用。例如 <student rollno = "393" /> |
| attribute-type | 属性的类型。例如,<xs:attribute name = "rollno" type = "xs:integer"/> 将属性类型定义为整数,rollno 的值为 int 类型。 <student rollno = "393" /> |
示例
考虑以下 XML 元素
<student rollno = "393" />
rollno 属性的 XSD 声明如下 -
<xs:attribute name = "rollno" type = "xs:integer"/>
默认值
可以为属性指定一个默认值。当属性没有值时,将使用默认值。
<xs:attribute name = "grade" type = "xs:string" default = "NA" />
固定值
可以为属性指定一个固定值。如果指定了固定值,则该元素不能有任何值。
<xs:attribute name = "class" type = "xs:string" fixed = "1" />
限制
默认情况下,属性是可选的。但是,要使属性成为强制性的,“use”属性可用。
<xs:attribute name = "rollno" type = "xs:integer" use = "required"/>
xsd_simple_types.htm
广告