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
廣告
© . All rights reserved.