XML Schema Tutorial - Part 5 XML Schema Tutorial - Part 5

Home > XML Tutorials > Part 5 - XSD Groups and Any Types

XSD Group, AttributeGroup, Any and AnyAttribute

This article covers some of the less frequently used constructs in the XML Schema Standard.

Element and Attribute Groups

Elements and Attributes can be grouped together using <xs:group> and <xs:attributeGroup>. These groups can then be referred to elsewhere within the schema. Groups must have a unique name and be defined as children of the <xs:schema> element. When a group is referred to, it is as if its contents have been copied into the location it is referenced from.

Note: <xs:group> and <xs:attributeGroup> can not be extended or restricted in the way <xs:complexType> or <xs:simpleType> can. They are purely to group a number of items of data that are always used together. For this reason they are not the first choice of constructs for building reusable maintainable schemas, but they can have their uses.

<xs:group name="CustomerDataGroup">
                <xs:element name="Forename" type="xs:string" />
                <xs:element name="Surname" type="xs:string" />
                <xs:element name="Dob" type="xs:date" />
<xs:attributeGroup name="DobPropertiesGroup">
        <xs:attribute name="Day" type="xs:string" />
        <xs:attribute name="Month" type="xs:string" />
        <xs:attribute name="Year" type="xs:integer" />
Defining Groups

These groups can then be referenced in the definition of complex types, as shown below:

<xs:complexType name="Customer">
                <xs:group ref="CustomerDataGroup" />
                <xs:element name="..." type="..." />
        <xs:attributeGroup ref="DobPropertiesGroup" />
Refernecing Groups

The <any> Element

The <any> construct allows us specify that our XML document can contain elements that are not defined in this schema. A typical use for this is when you define a message envelope. For example, the message payload is unknown to the system, but we can still validate the message.

Look at the following schema:

<xs:element name="Message">
                        <xs:element name="DateSent" type="xs:date" />
                        <xs:element name="Sender" type="xs:string" /> 
                        <xs:element name="Content">
                                                <xs:any />

We have defined an element called "Message", which must have a "DateSent" child element (which is a date), a "Sender" child element (which must be a string), and a "Content" child element - which can contain any element - it doesn't even have to be described in the schema.

So the following XML would be acceptable.


The <any> construct has a number of properties that can further restrict what can be used in its place.

minOccurs and maxOccurs allows you to specify how may instances of undefined elements must be placed within the XML document.

namespace allows you to specify that the undefined element must belong to a given namespace. This may be a list of namespace's (space separated). There are also three built in values ##any, ##other, ##targetnamespace, ##local. Consult the XSD standard for more information on this.

processContents tells the XML parser how to deal with the unknown elements. The values are:

  • Skip - no validation is performed - but it must be well formed XML.
  • Lax - if there is a schema to validate the element, then it must be valid against it, if there is no schema, then that's OK.
  • Strict - There must be a definition for the element available to the parser, and it must be valid against it.

The <anyAttribute> Element

<anyAttribute> works in the same way as <any>, except it allows unknown attributes to be inserted into a given element.

<xs:element name="Sender">
                        <xs:extension base="xs:string">
                                <xs:anyAttribute />

This would mean that we can add any attributes we like to the Sender element, and the XML document would still be valid:

<Sender ID="7687">Fred</Sender>
< Prev | 1 | 2 | 3 | 4 | 5 | Next >

Video Tutorials

VIEW TUTORIAL XML Schema Editor Overview

XML Schema Editor Overview

This video overview shows how to work in the graphical, text and split views, adding items, changing properties and updating documentation annotations.

VIEW TUTORIAL Generate a Sample XML Document

Generate a Sample XML Document

This video tutorial shows you how to create a sample XML document from an industry standard schema (Google Site Map).

VIEW TUTORIAL Generate HTML Documentation

Generate HTML Documentation

This video tutorial shows how to create HTML documentation for an XML Schema.


XML Data Binding

This video tutorial shows you how to generate strongly typed API source code from your XSD using the Liquid XML Data Binder.


XML Data Mapper

This video tutorial shows how to create a mapping transform, using the graphical drag and drop interface, to convert XML data of different XML Schema.

Make working with XSD easy using Liquid XML Studio Free Trial

Liquid XML Studio Editors and Debuggers


Graphical XML Schema Editor



Graphical XML Editor


FEATURE DETAILS XSLT Editor and Debugger

XSLT Editor and Debugger


FEATURE DETAILS XQuery Editor and Debugger

XQuery Editor and Debugger