1. Technology

The Dos and Don'ts of an Attribute in an XML Schema

Stepping Through an XML Schema


An XML schema considers attributes as separate entities. In the world of schema design, the handling of an attribute works much the same as a simple element. When stepping through schema creation, you must understand the dos and don'ts of an attribute.

Do Write the Attribute using Simple Element Syntax

A simple element in an XML schema contains the namespace, element name and data type.

<xs:element name= "message" type= "xs:string" />

This basic formula works with defining an attribute too.

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

Attributes offer information to the processor regarding style and validation. For instances, if the XML data string requires the English language, the code for this attribute might look like this:

XML element: <message lang= "EN">Hi, Jane </message>
Corresponding Schema definition:
<xs:attribute name= "lang" type= "xs:string"/>

Don't List an Attribute as Part of an Element


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

is incorrect. A schema is meticulous. It wants to consider the attribute as a separate tag and insists you list it individually.

Don't Forget the Data Type

Data types are optional, but as your XML documents become more complex, you want to get in the habit of using data types for validation. Data types for attributes work the same as they do for any element in an XML schema. String, date, time, integer and Boolean are all common data types. Creating a data type requires you to use the tag 'type=' and name the data type with the 'xs' namespace.

Do Define the Data Type in an Attribute if Necessary for Validation

The rules for data types and validation are the same for an attribute as they are for an element. Default means the parser will fill in the attribute if none is listed in the XML code.

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

Fixed identifies an attribute that must always read the same way.

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

Since an attribute is an optional component in XML, you can instruct the processor to require an attribute in the document with 'use= "required"'.

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

Important Terms in this Article

An instruction provided to explain style and/or validation for the element.

Simple Element
An element with only text, simple elements do not contain attributes or nested elements.

Data Type
A data type defines the element based on the format of the text and supplies validation criteria for the parser.

Fixed establishes a set value.

Default establishes what the value will read if the XML code does not list a value.

If all the discussion about breaking down a simple element has you confused, look for How to Break Down a Simple Element by Data Type on my tutorial page.

The next article will put everything together for an XML schema including the declaration statement, classification of elements, namespaces, data types and attributes. If you need to catch up on this series, Stepping through an XML schema, start at the first article How to Write Your First XML Schema.

XML Schema Tutorial

  1. About.com
  2. Technology
  3. Web Design / HTML
  4. XML
  5. Schema
  6. The Dos and Don'ts of an Attribute in an XML Schema

©2014 About.com. All rights reserved.