XML - (XML Schema Definition|XSD) (Schema With Capital S)

Card Puncher Data Processing

About

The Data Modeling - What is a schema ? in XML.

XML Schema definition language (Xsd) is a language that define an XML schema (usually written in the W3C XML Schema Language). It provides functionality above and beyond what is provided by DTD.

The XML Schema language provides the elementary data types for numbers, booleans, strings, URIs, dates and times, and for references and other XML constructs. User-defined data types can be derived from any elementary type by adding one or more restrictions. This is done by adding so-called facets, which are available to set lower or upper bounds for values or string lengths, to limit the precision, to enumerate all legal values, and to define a pattern for a string type.

Form

The tags of schema elements are presented in the qualified form, with xsd as the namespace identifier. This (or some other name, e.g., xs) identifier must be bound to the URI identifying the XML Schema language.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
            ....
</xsd>

Each of the elements in the schema has a prefix xsd: which is associated with the XML Schema namespace through the declaration, xmlns:xsd=“http://www.w3.org/2001/XMLSchema”, that appears in the schema element. The prefix xsd: is used by convention to denote the XML Schema namespace, although any prefix can be used

Declaration

A schema consists of:

They determine the appearance of elements and their content in instance documents.

Element

The xsd:element defines the XML tag.

Elements are declared using the element element. Example:

<xsd:element name="name"     type="xsd:string"/>
<xsd:element name="zip"      type="xsd:decimal"/>
<xsd:element name="Quantity" type="xsd:int"/>

It will result for instance in the following XML instance:

<name>Nico</name>
<zip>3234</zip>
<Quantity>1</Quantity>

complexType

complexType elements are elements that:

  • contain sub-elements (allow elements in their content)
  • may carry attributes

New complex types are defined using the complexType element which contain a set of:

  • element declarations,
  • element references,
  • and attribute declarations.

For example, USAddress is defined as a complex type, and within the definition of USAddress we see five element declarations and one attribute declaration:

<xsd:complexType name="USAddress" >
  <xsd:sequence>
    <xsd:element name="name"   type="xsd:string"/>
    <xsd:element name="street" type="xsd:string"/>
    <xsd:element name="city"   type="xsd:string"/>
    <xsd:element name="state"  type="xsd:string"/>
    <xsd:element name="zip"    type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
</xsd:complexType>

Example of Instance for the USAddress complextype:

<shipTo country="US">
    <name>Alice Smith</name>
    <street>123 Maple Street</street>
    <city>Mill Valley</city>
    <state>CA</state>
    <zip>90952</zip>
</shipTo>

simpleType

simpleType elements are elements that:

  • do not contain any sub-elements (cannot have element content)
  • cannot carry attributes
  • contain numbers (and strings, and dates, etc.)

Some elements have attributes; attributes always have simple types.

Simple types are defined as part of XML Schema's repertoire of built-in simple types such as:

  • string,
  • decimal
  • NMTOKEN

attribute

attributes are declared using the attribute element.

<xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>

Grouping facilities

For child elements, the schema's element grouping facilities

, , and in combination with repetition limit attributes let you define XML structures that are the equivalent of the classic concepts of array, list, structure (or record) and union.
Association Instance and Schema

An instance is not required to reference a schema.

Snippet
Integer Range Type

The XML Schema snippet shown below defines an integer type, limiting permissible values to the range 1 to 255.

<xsd:simpleType name="GroupType">
  <xsd:restriction base="xsd:int">
    <xsd:minInclusive value="1"/>
    <xsd:maxInclusive value="255"/>
  </xsd:restriction>
</xsd:simpleType>
String with Length Limits

The XML Schema snippet shown below defines a string type, limiting string lengths to the range 1 to 3.

<xsd:simpleType name="ShortNameType">
  <xsd:restriction base="xsd:string">
    <xsd:minLength  value="1"/>
    <xsd:maxLength  value="3"/>
  </xsd:restriction>
</xsd:simpleType>
Defining a String Type Restricted By Pattern

A string type may also be restricted by a pattern facet. The example below defines a type for strings of arbitrary length consisting of 'L' and 'R' only.

<xsd:simpleType name="DirType">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="[LR]*"/>
  </xsd:restriction>
</xsd:simpleType>

The syntax for regular expressions in pattern facets provides the basics for repetition, alternatives and grouping. It should be noted that a regular expression is always matched against the entire string value.

Defines an element containing sub-elements

The XML schema snippet shown below defines an element containing sub-elements with xsd:date and xsd:time.

<xsd:complexType name="DateTimeType">
  <xsd:sequence>
    <xsd:element name="Date" type="xsd:date"/>
    <xsd:element name="Time" type="xsd:time"/>
  </xsd:sequence>
 </xsd:complexType>
Defines an element containing binary data

The XML Schema datatype to use for binary data is xsd:hexBinary. A sample schema declaration is shown below.

<xsd:complexType name="BinaryType">
  <xsd:sequence>
    <xsd:element name="data" type="xsd:hexBinary"/>
  </xsd:sequence>
</xsd:complexType>
Defining a List of Integers

To obtain a simple type that can be used for XML element values as well as for attribute values consisting of a simple series of values, use an xsd:list

<xsd:simpleType name="NumberListType">
  <xsd:list itemType="xsd:int"/>
</xsd:simpleType>
Defining a List of String

The XML representation of a xsd:list is a list of values of the defined type, separated by white space.

For string unless you can be very sure that your strings are free from spaces or any other white space, don't do anything like this:

<xsd:simpleType name="StringListType">
  <xsd:list itemType="xsd:string"/>   <!-- dangerous! -->
</xsd:simpleType>

To be on the safe side, use a complex type that contains a sequence of string elements.

Defining an Enumeration

If you want a data type that enumerates discrete values you should use a restriction of the schema type xsd:string, enumerating all the values as you would in a Java enum type.

<xsd:simpleType name="IXLType">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="eStwA"/>
    <xsd:enumeration value="eStwS"/>
    <xsd:enumeration value="SpDrL"/>
    <xsd:enumeration value="SpDrS"/>
    <xsd:enumeration value="VGS80"/>
  </xsd:restriction>
</xsd:simpleType>
Defining Types for XML Elements Without Content

Types for XML elements are constructed using xsd:complexType, even if they do not have content. The snippet below defines a simple element with two attributes and no sub-elements.

<xsd:complexType name="RouteType">
  <xsd:attribute name="Pos" type="xsd:int" use="optional" default="1"/>
  <xsd:attribute name="Dir" type="DirType" use="required"/>
</xsd:complexType>
Defining an Ordered Set of Elements (xsd:sequence)

The schema element xsd:sequence defines that the enclosed set of elements should occur in the given order and according to the specified minimum and maximum repetition counts. (The default for both is 1.) The following complex type defines a set of two coordinates.

<xsd:complexType name="PointType">
  <xsd:sequence>
    <xsd:element name="X" type="xsd:int"/>
    <xsd:element name="Y" type="xsd:int"/>
  </xsd:sequence>
</xsd:complexType>
Defining an Unordered Set of Elements (xsd:all)

Content consisting of a set of elements that may occur in any order within its parent XML element can be defined by using the schema element xsd:all. There is, however, a severe restriction: the maxOccurs may not have a value greater than 1. Here is the definition for an XML element describing the courses of a dinner which does not permit repetitions of any course, but you may omit all courses except for the main dish.

<xsd:complexType name="DinnerType">
  <xsd:all>
    <xsd:element name="Starter" type="xsd:string" minOccurs="0"/>
    <xsd:element name="Soup"    type="xsd:string" minOccurs="0"/>
    <xsd:element name="Entree"  type="xsd:string"/>
    <xsd:element name="Dessert" type="xsd:string" minOccurs="0"/>
  </xsd:all>
</xsd:complexType>
Defining Alternative Elements (xsd:choice)

The schema element xsd:choice lets you define a type for an XML element which has a content of exactly one element from a given set of alternatives.

<xsd:complexType name="CommType">
  <xsd:choice>
    <xsd:element name="SMS"   type="xsd:string"/>
    <xsd:element name="MMS"   type="xsd:string"/>
    <xsd:element name="Email" type="xsd:string"/>
  </xsd:choice>
</xsd:complexType>
Defining a Homogeneous List of Elements

To define an element where some sub-element occurs repeatedly, we make use of the optional attributes minOccurs and maxOccurs. Various combinations are possible, permitting the definition of a list that may be empty, may contain any number of elements, or a fixed number. The definition of an unbounded list with at least two elements is given below. (PointType is shown in subsection Content: An Ordered Set of Elements.)

<xsd:complexType name="PolygonType">
  <xsd:sequence>
    <xsd:element name="Points" type="PointType"
                 minOccurs="2" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>
Documentation / Reference





Discover More
Card Puncher Data Processing
JPA - orm.xml (Mapping Descriptor files)

orm.xml is a deployment descriptors describing the object-relational mapping. The Xsd Schema file can be found: orm_2_0.xsd or in the specification...
Card Puncher Data Processing
JPA - persistence.xml

The JPA specification requires the use of a persistence.xml file for deployment. The persistence configuration file must be named “META-INF/persistence.xml” in the persistence archive. This file...
Hyperjaxb Generated Source
Java - HyperJaxb - From Xml (Jaxb) to database (Jpa)

HyperJaxb is a Jaxb plugin that creates classes that are: JAXB-enabled (thanks to the JAXB schema compiler) and JPA-enabled (thanks to the Hyperjaxb3 Jaxb plugin) HyperJaxb permits then to have...
Java Conceptuel Diagram
Java - Java API for XML Binding (JAXB) - (Un)Marshaller

API JAXB is a serialization library (Java binding tool) between XML and Object. It uses annotations to bind XML documents to a java object model. Located under the javax.xml.bind package. It generates...
Webserviceexample
Web Service - Simple Object Access Protocol (SOAP)

SOAP (Simple Object Access Protocol) is a Web Services protocol for exchanging: Extensible Markup Language (XML) via the following Application Layer protocols: Remote Procedure Call (RPC) and HTTP...
Weblogic Architecture Domain
Weblogic - Domain

An Oracle WebLogic Server administration domain is a logically related group of Java components: the Administration Server that you use for configuration and management purposes only. and the Managed...
Card Puncher Data Processing
XML - schema (without capital S)

A schema contains a set of rules that constrains the structure and content of an xml document, i.e., its elements, attributes, and text. A schema is, in other words, a specification of the syntax and...
Card Puncher Data Processing
Xml - Namespace (xmlns)

namespace in XML are used to be able to have the same node name but in different namespace. A simple example would be to consider an XML document that contained references to: a customer and...



Share this page:
Follow us:
Task Runner