Use of Schema Attributes in XML

This article describe about Schema Attributes in XML.
  • 1739

Schema Attributes

All element in the schema does not contain attributes. If element contain the attributes then it become complex element. But we can declared attributes itself simple type

How to define attributes

<xs:attribute name="pqr" type="xyz"/>

Note: Here declared attributes pqr  is the name of attributes and xyz is data type of attributes

Data type of attributes

XML support many different type of data type for attributes likes

  • xs:boolean
  • xs:date
  • xs:time
  • xs:string
  • xs:decimal
  • xs:integer

Example

 <firstname lang="EN">satya</firstname>

Note: This is the example of XML element with attributes

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

Note: This is the corresponding definition of attributes

Default and Fixed Values for Attributes

Attributes may also contain default OR a fixed  value which is given at the time of creation.

Default value automatically define when no any value define.

Below given example default value is "EN"

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

Note: This is the example of attributes that contain default value "EN"

Attributes also contain fixed value

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


Note: In this example attributes contain the fixed value

Optional and Required Attributes

When attributes does not define explicitly then by default it contain default value which are optional. And when explicitly define attributes then required, use the "use" attributes.

Example

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

Restrictions on Content

We all already aware about XML data type. XML element and attributes has the data type and when XML data type define for the element OR attributes. It take restriction on the element OR attributes. because it contain data only it's define data type.

For example

If an XML attribute is the type of "xs:integer"  and assign it's value "Hello India" it give the error because it is not valid this attribute can contain only integer value. In XML schema we can also add our own restriction this type of restriction is called facets OR custom restriction.

further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.