A Simple Overview of W3C XML Schema

download A Simple Overview of W3C XML Schema

of 20

Transcript of A Simple Overview of W3C XML Schema

  • 8/2/2019 A Simple Overview of W3C XML Schema

    1/20

    A Simple Overview of W3C XML SchemaVersion 0.91

    Copyright 2007 Codalogic Ltd.

    IntroductionThis tutorial is intended to give a brief introduction to W3C XML Schema. It is not intended to cover all aspects of XML Schema, although it is hoped that after reading this you will be able to do useful work using Schema.

    The focus of this tutorial is on the data-oriented use of XML Schema, with the intention that it gives a grounding in XMLSchema that will be applicable to using tools such as LMX. Further tutorials in this series may be added from time totime on the LMX Support Page .

    For a fuller description of XML Schema, we recommend the XSD Primer and the book XML Schema by Eric VanDer Vlist available at Amazon.com and Amazon.co.uk .

    Advert:

    Contents1 - Prerequisites

    2 - Approach3 - What does XML Schema Look Like?4 - Schema Preamble5 - Defining Elements (Part 1)6 - Specifying How Many Times an Element Can Appear 7 - Specifying How Many Times an Attribute Can Appear 8 - Specifying Your Own Types 8.1 - Adding Attributes to a Type with a Simple Type Body 8.2 - Defining Types that Contain Multiple Elements 8.3 - Defining Types that Contain One of a Selection of Elements 8.4 - Specifying an Empty Element9 - Defining More Restricted Simple Types 9.1 - Same Simple Type, Different Name10 - Defining Elements (Part 2) - Defining Elements using Local Types11 - Documenting Your Schema12 - Putting It All Together 13 - Simple Steps to Writing a Schema14 - Experimenting with Schemas15 - Things We've Not Covered16 - Please Rate This Article

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 1/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    2/20

    16 - Please Rate This Article

    [Top] [Contents]

    1 - Prerequisites

    Before reading this guide you should have a basic understanding of how XML represents data. It is also advantageousto understand XML namespaces. For further information on these topics try looking for other tutorials in this series onthe LMX Support Page .

    [Top] [Contents]

    2 - ApproachIt can be very difficult to learn XML Schema completely. Hence, this guide only sets out to give you a basic workingknowledge of XML Schema.

    To make it easier to use XML Schema for your own purposes this guide is designed so that you can cut-and-pastesnippets of it into your own document. As such, most examples are of the form, "If you want 'X' in your XML instance,do 'Y' in your Schema."

    Some of the fields in the examples need to be customised to your application. Such fields are generally called"MyThing". For example, MyElement, MyType, MyDomain, MySchema etc. If there is more than one type of 'thing' inan example, then a number will be appended to them to differentiate them. For example, MyElement1, MyElement2,MyElement3 etc. You should change these to more appropriate names if you paste the snippets into your Schema.

    Some snippets require additional content to make them complete. Where this is the case, we will include ellipsis (...),

    perhaps with a comment, to signify this.

    We won't necessarily explain everything in detail. Where we have not explained something sufficiently, we suggestsimply copying the un-described fragment to your application along with the other parts that are more fully described.

    [Top] [Contents]

    3 - What does XML Schema Look Like?Before we move into the details, we first show you an example of an XML Schema. The reason for doing this is not so

    that you can immediately understand it, but so that you can get an idea of what this guide is aiming for you to be able tounderstand.

    The schema definition we are going to show you is a hypothetical example of storing an application's configuration and isas follows:

    < xs:schema targetNamespace ="http://xml2cpp.com/config.xsd"

    xmlns ="http://xml2cpp.com/config.xsd" xmlns:xs ="http://www.w3.org/2001/XMLSchema"

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 2/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    3/20

    elementFormDefault ="qualified" >

    < xs:element name ="config" type ="ConfigType"/>

    < xs:complexType name ="ConfigType">< xs:sequence >

    < xs:element name ="userName" type ="xs:string" minOccurs ="0"/>< xs:element name ="maxRecentProjects" type ="Int1To10Type"/>

    < xs:element name ="recentProject" type ="xs:string"minOccurs ="0" maxOccurs ="10"/>< xs:element name ="childWindow" type ="WindowType"

    minOccurs ="0" maxOccurs ="unbounded"/>< xs:element name ="dictionary" type ="DictionaryFileType"

    minOccurs ="0"/>

    < xs:simpleType name ="Int1To10Type">< xs:restriction base ="xs:int">

    < xs:minInclusive value ="1"/>< xs:maxInclusive value ="10"/>

    < xs:complexType name ="WindowType">< xs:attribute name ="name" type ="xs:string" use ="required"/>< xs:attribute name ="width" type ="xs:unsignedInt" use ="required"/>< xs:attribute name ="height" type ="xs:unsignedInt" use ="required"/>

    < xs:complexType name ="DictionaryFileType">< xs:simpleContent >

    < xs:annotation >< xs:documentation >The base type is the name of the dictionary file.< xs:extension base ="xs:string">

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

    From the above definition you might be able to pick out that something named config is defined to be an element thathas the type ConfigType . From that you might deduce that an instance of that element would look something like:

    < config ...>...

    You may also infer that the entity called ConfigType defines a type that contains multiple child elements.

    You may also be able to surmise other features about a valid XML instance of the Schema. But don't worry if you can'tat this stage as it will hopefully become clearer soon.

    For the record, an example XML instance conforming to above Schema is as follows:

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 3/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    4/20

    < config xmlns ="http://xml2cpp.com/config.xsd">< userName >John Smith< maxRecentProjects >10

    < recentProject >z:\projects\project1.prj< recentProject >z:\projects\project5.prj< recentProject >z:\projects\project3.prj< recentProject >z:\projects\project2.prj

    < childWindow name ="Input" width ="200" height ="100"/>< childWindow name ="Output" width ="200" height ="150"/>< childWindow name ="Transformation" width ="300" height ="100"/>

    < dictionary language ="en">z:\projects\dictionary.dic

    Now, let's dive in!

    [Top] [Contents]

    4 - Schema PreambleAn XML Schema contains some basic preamble and boilerplate material specifying global details about the schema.

    If you are assigning your Schema to a namespace (recommended) then the basic outline of a Schema is:

    < xs:schema targetNamespace ="http://MyDomain.com/MySchema.xsd" xmlns ="http://MyDomain.com/MySchema.xsd" xmlns:xs ="http://www.w3.org/2001/XMLSchema" elementFormDefault ="qualified" >

    ...element definitions...

    ...type definitions...

    The first thing to note is that the schema definition is defined in an xs:schema element.

    The targetNamespace attribute specifies the namespace to which the schema is being assigned. The xmlns attributefurther assigns the target namespace to be the default namespace.

    The xmlns:xs specifies that the schema language directives are assigned the xs: namespace prefix. Thus, the schemalanguage directives become xs:schema , xs:element , xs:attribute , xs:complexType , and so on.

    If you are familiar with how XML namespaces work, you can assign the various namespaces to different namespace

    prefixes if you desire. (For example, the XML Schema namespace is also commonly assigned to the xsd: namespace prefix. We save ourselves the extra typing!)

    The elementFormDefault attribute declares that all element names in an XML instance should be qualified with (i.e.associated with) a namespace, either via a namespace prefix (e.g. MyNSPrefix:MyElement ) or the defined defaultnamespace (e.g. MyElement ).

    The resulting XML instance would be of the form:

    < MyElement xmlns ="http://MyDomain.com/MySchema.xsd" ...>

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 4/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    5/20

    ...more to go here...

    If you are not assigning your schema to a namespace, then the outline is:

    < xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" >...element definitions......type definitions...

    This will result in an XML instance similar to:

    < MyElement ...>...more to go here...

    [Top] [Contents]

    5 - Defining Elements (Part 1)An element can be defined using an xs:element element of the form:

    < xs:element name ="MyElement" type ="...Type..."/>

    The type part can be one of the (simple) types defined in the XML Schema specifications (such as xs:int, xs:string etc.)or a type defined elsewhere in your schema.

    The types defined by XML Schema are:

    Schema Type Description

    xs:boolean A Boolean value.

    xs:string A string; typically Unicode

    xs:byte A signed 8-bit number.

    xs:short A signed 16-bit number.

    xs:int A signed 32-bit number.

    xs:long A signed 64-bit number.

    xs:unsignedByte An unsigned 8-bit number.

    xs:unsignedShort An unsigned 16-bit number.

    xs:unsignedInt An unsigned 32-bit number.

    xs:unsignedLong An unsigned 64-bit number.

    xs:integer, xs:nonPositiveInteger,xs:negativeInteger,xs:nonNegativeInteger,xs:positiveInteger

    Unbounded or partially bounded integers. It isrecommended that these types are avoided in schemasthat are intended to be used with binding tools.

    xs:decimalA decimal number that includes a fractional part but is notspecified using an exponent; for example, 123.45.

    xs:float, xs:double Single and double precision floating point numbers.

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 5/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    6/20

    xs:hexBinary, xs:base64Binary Binary data.

    xs:durationFor specifying elapsed time, particularly in terms of days,months, years etc.

    xs:dateTime, xs:time, xs:date,xs:gYearMonth, xs:gYear,xs:gMonthDay, xs:gDay,xs:gMonth

    Date and time related types.

    xs:anyURI, xs:QName,xs:NOTATION,xs:normalizedString, xs:token,xs:language, xs:ID,xs:IDREF, xs:IDREFS,xs:ENTITY, xs:ENTITIES,xs:NMTOKEN,xs:NMTOKENS, xs:Name,xs:NCName

    Other schema defined types.

    Table 1: Simple Types defined by XML Schema

    To use a schema defined simple type, you would do:

    < xs:element name ="MyElement" type ="xs:int"/>

    This would appear in an XML instance as something like:

    < MyElement >123

    A complete schema defining such an element might appear as:

    < xs:schema targetNamespace ="http://MyDomain.com/MySchema.xsd" xmlns ="http://MyDomain.com/MySchema.xsd" xmlns:xs ="http://www.w3.org/2001/XMLSchema" elementFormDefault ="qualified" >

    < xs:element name ="MyElement" type ="xs:int"/>

    To specify an element that uses a type defined in your schema, you would do:

    < xs:element name ="MyElement" type ="MyType"/>

    You can also define your own types locally to the xs:element definition. We will return to this later in 10 - DefiningElements (Part 2) - Defining Elements using Local Types .

    [Top] [Contents]

    6 - Specifying How Many Times an Element CanAppear

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 6/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    7/20

    Depending on your requirements, an element may be mandatory, optional, or be able to appear many times. TheminOccurs and maxOccurs attributes in an xs:element definition specify this. (Not surprisingly, minOccurs specifiesthe minimum number of times an element can appear and maxOccurs specifies the maximum number of times anelement can appear.)

    minOccurs can be assigned any non-negative integer value (e.g. 0, 1, 2, 3... etc.), and maxOccurs can be assigned anynon-negative integer value or the string constant " unbounded ".

    The default values of minOccurs and maxOccurs is 1 .

    So if both the minOccurs and maxOccurs attributes are absent, as shown in the following snippet, the element canappear once and once only:

    < xs:element name ="MyElement" type ="MyType"/>

    To specify that an element is optional (i.e. it may or may not be present in the XML instance), do:

    < xs:element name ="MyElement" type ="MyType" minOccurs ="0"/>

    To specify that an element can be absent, but can also appear an unlimited number of times, do:

    < xs:element name ="MyElement" type ="MyType"minOccurs ="0" maxOccurs ="unbounded"/>

    To specify that an element must appear at least once, but may also appear many times do:

    < xs:element name ="MyElement" type ="MyType" maxOccurs ="unbounded"/>

    Naturally it is OK to include the minOccurs and maxOccurs attributes with their default values, e.g.:

    < xs:element name ="MyElement" type ="MyType"minOccurs ="1" maxOccurs ="1"/>

    Or:

    < xs:element name ="MyElement" type ="MyType"minOccurs ="1" maxOccurs ="10"/>

    For more specific constraints on the occurrence, you can do things like:

    < xs:element name ="MyElement" type ="MyType"minOccurs ="0" maxOccurs ="10"/>

    and:

    < xs:element name ="MyElement" type ="MyType"minOccurs ="8" maxOccurs ="27"/>

    [Top] [Contents]

    7 - Specifying How Many Times an Attribute CanAppear

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 7/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    8/20

    We haven't discussed defining attributes yet, but while we're discussing how many times things can appear, it seemsappropriate to address this in relation to attributes now. (We will discuss further details about attributes later.)

    Attributes can either be present or absent. A particular attribute cannot appear multiple times within a single element.

    The use attribute within an xs:attribute definition is used to specify how many times an attribute can appear.

    By default an attribute is optional, and may or may not appear in a XML instance. To specify this, use the form:< xs:attribute name ="MyAttribute1" type ="...simple type..."/>

    This is the same as saying:

    < xs:attribute name ="MyAttribute1" type ="...simple type..." use ="optional"/>

    To specify that an attribute must be present, use the form:

    < xs:attribute name ="MyAttribute1" type ="...simple type..." use ="required"/>

    For completeness, in some cases (for complex type restriction, which we do not cover in this tutorial) it is useful tospecify that an attribute must not be present. For this case, use the form:

    < xs:attribute name ="MyAttribute1" type ="...simple type..." use ="prohibited"/>

    [Top] [Contents]

    8 - Specifying Your Own Types

    We have already shown in 5 - Defining Elements (Part 1) how to define an element that only contains one of the simpletypes defined by XML Schema. Such elements appear like this in an XML instance:

    < MyElement >123

    and are specified in your schema something like this:

    < xs:element name ="MyElement" type ="xs:int"/>

    In this section we define how to define your own types that combine the various Schema defined simple types in muchthe same way as you would combine the C++ built in types (such as int ) into classes, structs and unions.

    [Top] [Contents]

    8.1 - Adding Attributes to a Type with a Simple Type Body

    If you want an element to appear in an XML instance as:

    < MyElement MyAttribute ="MyData">123

    Then you need to have schema of the form:

    < xs:complexType name ="MyType">

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 8/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    9/20

    < xs:simpleContent >< xs:extension base ="...the simple base type to appear in the body...">

    < xs:attribute name ="MyAttribute1" type ="...simple type..."/>< xs:attribute name ="MyAttribute2" type ="...simple type..."/>

    For example:< xs:complexType name ="MyType">

    < xs:simpleContent >< xs:extension base ="xs:int">

    < xs:attribute name ="MyAttribute1" type ="xs:string"/>< xs:attribute name ="MyAttribute2" type ="xs:string"/>

    For the record, this is called a complex type with simple content. (Complex type because it contains multiple datavalues, and simple content because the content of the element body is a simple type.)

    The above example can be interpreted as saying, a new type is to be created that has a body of type xs:int (specified by the base attribute) that is extended by the addition of attributes to form a complex type with simple content.

    See 7 - Specifying How Many Times an Attribute Can Appear for further information on how to specify how often anattribute can occur.

    [Top] [Contents]

    8.2 - Defining Types that Contain Multiple Elements

    To define a type that allows you to define elements of the form:< MyElement >

    < MyChildElement1 >123< MyChildElement2 >Text< MyChildElement3 >123.45

    Your schema should look like:

    < xs:complexType name ="MyType">< xs:sequence >

    < xs:element name ="MyChildElement1" type ="...type..."/>

    < xs:element name ="MyChildElement2" type ="...type..."/>< xs:element name ="MyChildElement3" type ="...type..."/>

    Here, the xs:sequence element is the key to specifying that all the elements can appear together. As such, this is oftendescribed as a sequence .

    This is similar to a struct in C/C++ and is called a complex type with complex content (because the body of the elementhas multiple elements).

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 9/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    10/20

    Note that, subject to occurrence constraints, within a sequence the elements must appear in an XML instance in thesame order that they appear within the schema definition.

    Each of the above elements can also have min and max occurrences defined as described in 6 - Specifying How ManyTimes an Element Can Appear . For example:

    < xs:complexType name ="MyType">

    < xs:sequence >< xs:element name ="MyChildElement1" type ="xs:int" minOccurs ="0"/>< xs:element name ="MyChildElement2" type ="xs:string" maxOccurs ="10"/>< xs:element name ="MyChildElement3" type ="xs:float"/>

    If you want to add attributes to your type, so that elements can appear as:

    < MyElement MyAttribute1 ="123" MyAttribute2 ="data">< MyChildElement1 >123< MyChildElement2 >Text< MyChildElement3 >123.45

    use the following style of schema definition:

    < xs:complexType name ="MyType">< xs:sequence >

    < xs:element name ="MyChildElement1" type ="...type..."/>< xs:element name ="MyChildElement2" type ="...type..."/>< xs:element name ="MyChildElement3" type ="...type..."/>

    < xs:attribute name ="MyAttribute1" type ="...type..."/>< xs:attribute name ="MyAttribute2" type ="...type..."/>

    The number of times each attribute can appear can be specified as described in 7 - Specifying How Many Times anAttribute Can Appear . This might yield something like:

    < xs:complexType name ="MyType">< xs:sequence >

    < xs:element name ="MyChildElement1" type ="xs:int" minOccurs ="0"/>< xs:element name ="MyChildElement2" type ="xs:string" maxOccurs ="10"/>< xs:element name ="MyChildElement3" type ="xs:float"/>

    < xs:attribute name ="MyAttribute1" type ="xs:int" use ="required"/>< xs:attribute name ="MyAttribute2" type ="xs:string"/>

    [Top] [Contents]

    8.3 - Defining Types that Contain One of a Selection of Elements

    You may wish to define a type that has multiple possible child elements defined, but only one of the specified childelements is allowed to be used at a time in any one instance of the type. This is similar to a union in C/C++ (although inthe case of schema, the name of the selected value is recorded as well as the value itself; thus sometimes called adistinguished union).

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 10/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    11/20

    For example, you may want your XML instance to appear as:

    < MyElement >< MyChildElement1 >123

    Or:

    < MyElement >< MyChildElement2 >Text

    but NEVER :

    < MyElement >< MyChildElement1 >123< MyChildElement2 >Text

    In this case your schema should look like:

    < xs:complexType name ="MyType">< xs:choice >< xs:element name ="MyChildElement1" type ="...type..."/>< xs:element name ="MyChildElement2" type ="...type..."/>

    The main difference between this and the case where multiple elements may appear is the presence of the xs:choiceelement. As you might expect, this construct is often called a choice .

    As with the multiple element form, the child elements can have their occurrences specified as described in 6 - SpecifyingHow Many Times an Element Can Appear , to give you a shema snippet of the form:

    < xs:complexType name ="MyType">< xs:choice >

    < xs:element name ="MyChildElement1" type ="xs:int" minOccurs ="0"/>< xs:element name ="MyChildElement2" type ="xs:string" maxOccurs ="10"/>

    If this is done, you might end up with an instance that looks like:

    < MyElement >< MyChildElement2 >ABC< MyChildElement2 >DEF< MyChildElement2 >GHI

    Or even (when an absent MyChildElement1 is chosen):

    < MyElement >

    Again, you can add attributes to this, to get XML instances of the form:

    < MyElement MyAttribute1 ="123" MyAttribute2 ="data">< MyChildElement1 >123

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 11/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    12/20

    by using the following style of schema definition:

    < xs:complexType name ="MyType">< xs:choice >

    < xs:element name ="MyChildElement1" type ="...type..."/>< xs:element name ="MyChildElement2" type ="...type..."/>< xs:element name ="MyChildElement3" type ="...type..."/>

    < xs:attribute name ="MyAttribute1" type ="...type..."/>< xs:attribute name ="MyAttribute2" type ="...type..."/>

    [Top] [Contents]

    8.4 - Specifying an Empty Element

    You may wish to specify an empty element of the form:

    < MyElement />

    There is no explicit way to do this in XML Schema. Instead an idiom can be used that is a complex type without anychild elements defined. Thus a schema definition for this is:

    < xs:complexType name ="MyEmptyType">

    Or equivalently:

    < xs:complexType name ="MyEmptyType"/>

    This is basically a stripped down version of the sequence definition described in 8.2 - Defining Types that Contain

    Multiple Elements . Observe however that even the xs:sequence part of the sequence definition is not required.

    You can of course add attributes to this type, that would allow instances of the form:

    < MyElement MyAttribute1 ="123" MyAttribute2 ="data"/>

    As you might exect from the previous material, this can be done as follows:

    < xs:complexType name ="MyType">< xs:attribute name ="MyAttribute1" type ="...type..."/>< xs:attribute name ="MyAttribute2" type ="...type..."/>

    [Top] [Contents]

    9 - Defining More Restricted Simple TypesThe XML Schema specification defines a number of simple types. However, in a number of cases you may wish to usemore restricted simple types. For example, you may wish to limit the range of an integer to specific values, and restrictthe length of a string. This section briefly describes how to do that.

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 12/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    13/20

    The simple types defined in XML Schema can be further restricted using what are called facets. The XML Schemaspecification defines which facets can be applied to each simple type.

    The XML Schema facets are: xs:length, xs:minLength, xs:maxLength, xs:pattern, xs:enumeration, xs:whiteSpace,xs:maxInclusive, xs:maxExclusive, xs:minExclusive, xs:minInclusive, xs:totalDigits, xs:fractionDigits. We will only explaina few of these facets here. The general form for restricting a simple type is:

    < xs:simpleType name ="MyNewType">< xs:restriction base ="...type being restrict...">

    ...restricting facets...

    Here, the type specified by the base attribute is the initial type from which the new type is being derived (by restriction).

    Perhaps the most useful types to restrict are the integers and xs:string .

    The most common restriction of an integer is to limit the range of valid values it can take. This can be done using thexs:maxInclusive , xs:maxExclusive , xs:minExclusive , xs:minInclusive facets. For example, to limit an

    integer to the range 0 < xs:minInclusive value ="0"/>< xs:maxInclusive value ="100"/>

    Or, to limit an integer to the range 0 < x < 100, you can do:

    < xs:simpleType name ="MyEx0to100IntType">< xs:restriction base ="xs:int">

    < xs:minExclusive value ="0"/>< xs:maxExclusive value ="100"/>

    Note that it is legal to restrict a value that has already been restricted. For example, you may choose to define a typethat is a restriction of xs:int to the range 0 to 100. You could then define a further type that restricted this new type tothe range 0 to 10. This could be done by doing:

    < xs:simpleType name ="My0to100IntType">< xs:restriction base ="xs:int">

    < xs:minInclusive value ="0"/>< xs:maxInclusive value ="100"/>

    < xs:simpleType name ="My0to10IntType">< xs:restriction base ="My0to100IntType">

    < xs:maxInclusive value ="10"/>

    Note that the order of definitions in XML schema is not important, so this could equally be written as:

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 13/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    14/20

    < xs:simpleType name ="My0to10IntType">< xs:restriction base ="My0to100IntType">

    < xs:maxInclusive value ="10"/>

    < xs:simpleType name ="My0to100IntType">< xs:restriction base ="xs:int">

    < xs:minInclusive value ="0"/>< xs:maxInclusive value ="100"/>

    The most common restrictions applied to strings is to their lengths, and the patterns they are allowed. Restricting a stringmay also be used to define an enumerated value.

    To restrict the length of a string, you can do:

    < xs:simpleType name ="My0to100CharStringType">< xs:restriction base ="xs:string">

    < xs:minLength value ="0"/>< xs:maxLength value ="100"/>

    To limit the valid patterns of a string, you can do:

    < xs:simpleType name ="MyPatternCharStringType">< xs:restriction base ="xs:string">

    < xs:pattern value ="ABC\d+"/>

    Here, the value of the value attribute contains a Perl-like regular expression. The main difference is that the specifiedregular expression is implicitly anchored to the beginning and end of the string being matched, so that the whole XMLinstance value must match the pattern, not just a fragment of it. Put another way, if converted to Perl, the pattern abovewould become ^ABC\d+$ .

    Multiple facets can be applied to a type at the same time, so you can do:

    < xs:simpleType name ="MyPattern0to10CharStringType">< xs:restriction base ="xs:string">

    < xs:minLength value ="0"/>< xs:maxLength value ="10"/>< xs:pattern value ="ABC\d+"/>

    In this case, all the constraints applied to a type using facets must all be valid in an XML instance.

    Another common case of applying restrictions to strings is forming enumerations. This can be done as:

    < xs:simpleType name ="MyGenderType">< xs:restriction base ="xs:string">

    < xs:enumeration value ="Female"/>< xs:enumeration value ="Male"/>

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 14/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    15/20

    With the above example, the string is only allowed to be "Female" or "Male". For example, if you have an elementdefinition of:

    < xs:element name ="MyElement" type ="MyGenderType"/>

    The only valid instances of this are:

    < MyElement >Female

    or:

    < MyElement >Male

    Note that with enumerations it is very important to consider what will happen when you want to upgrade your schema tothe next version. Using the form above, it would be illegal to add an additional enumeration value in a subsequentversion of the schema. (Check for other tutorials in our series for how this might be made possible.) Thus it is importantto make sure that when using the definition method described here, the enumerated values are a closed set that will

    provably not need extending at a later date.

    [Top] [Contents]

    9.1 - Same Simple Type, Different Name

    Occasionally it is desirable to have types that have the same properties, but have different names. For example, asession identifier type may just be a regular xs:int. There is no special method for declaring this in Schema. Instead, youneed to use the restriction method described above, but not actually add any restrictions! For example:

    < xs:simpleType name ="SessionIdentifierType">< xs:restriction base ="xs:int"/>

    [Top] [Contents]

    10 - Defining Elements (Part 2) - Defining Elementsusing Local TypesFor simplicity we have chosen to separate definition of elements from the definition of types. Element definitions are thenlinked to type definitions using the form:

    < xs:element name ="MyElement" type ="...Type..."/>

    In general, this is a good approach.

    You may wish to combine a type definition with an element definition. This is called a local type.

    To define a local type you basically remove the type attribute from the element definition, remove the name attributefrom the type definition, and move the type definition into the body of the element definition. For example, instead of:

    < xs:element name ="MyElement" type ="MyGenderType"/>

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 15/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    16/20

    < xs:simpleType name ="MyGenderType">< xs:restriction base ="xs:string">

    < xs:enumeration value ="Female"/>< xs:enumeration value ="Male"/>

    You use:

    < xs:element name ="MyElement">< xs:simpleType >

    < xs:restriction base ="xs:string">< xs:enumeration value ="Female"/>< xs:enumeration value ="Male"/>

    [Top] [Contents]

    11 - Documenting Your SchemaTypically you'll want to add some documentation to your Schema. XML Schema allows a couple of ways to do this.The first method you can use is standard XML comments, such as:

    < xs:element name ="MyElement" type ="MyType"/>

    Such comments are easy to add, but are not strictly associated with the items they document.

    XML Schema includes another way of documenting schema components by using the xs:annotation andxs:documentation elements. These elements can only appear at certain places within a schema, typically as the firstelement after a main 'keyword' such as xs:schema , xs:element , xs:attribute , xs:complexType , andxs:simpleType . For example:

    < xs:element name ="MyElement" type ="MyType">< xs:annotation >< xs:documentation >

    Some useful information.

    < xs:attribute name ="MyAttribute" type ="MyType">< xs:annotation >< xs:documentation >

    Some useful information.

    < xs:complexType name ="MyType">

    < xs:annotation >< xs:documentation >Some useful information.

    ...

    < xs:simpleType name ="MyType">

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 16/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    17/20

    < xs:annotation >< xs:documentation >Some useful information.

    ...

    [Top] [Contents]

    12 - Putting It All TogetherWe mentioned in 4 - Schema Preamble that a schema has the form:

    < xs:schema targetNamespace ="http://MyDomain.com/MySchema.xsd" xmlns ="http://MyDomain.com/MySchema.xsd" xmlns:xs ="http://www.w3.org/2001/XMLSchema" elementFormDefault ="qualified" >

    ...element definitions...

    ...type definitions...

    Now more about the nature of element and types definitions has been covered, we can present a more completeexample of a schema. For this we reproduce the example given earlier.

    < xs:schema targetNamespace ="http://xml2cpp.com/config.xsd"

    xmlns ="http://xml2cpp.com/config.xsd" xmlns:xs ="http://www.w3.org/2001/XMLSchema" elementFormDefault ="qualified" >

    < xs:element name ="config" type ="ConfigType"/>

    < xs:complexType name ="ConfigType">

    < xs:sequence >< xs:element name ="userName" type ="xs:string" minOccurs ="0"/>< xs:element name ="maxRecentProjects" type ="Int1To10Type"/>< xs:element name ="recentProject" type ="xs:string"

    minOccurs ="0" maxOccurs ="10"/>< xs:element name ="childWindow" type ="WindowType"

    minOccurs ="0" maxOccurs ="unbounded"/>< xs:element name ="dictionary" type ="DictionaryFileType"minOccurs ="0"/>

    < xs:simpleType name ="Int1To10Type">

    < xs:restriction base ="xs:int">< xs:minInclusive value ="1"/>< xs:maxInclusive value ="10"/>

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 17/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    18/20

    < xs:complexType name ="WindowType">

    < xs:attribute name ="name" type ="xs:string" use ="required"/>< xs:attribute name ="width" type ="xs:unsignedInt" use ="required"/>< xs:attribute name ="height" type ="xs:unsignedInt" use ="required"/>

    < xs:complexType name ="DictionaryFileType">

    < xs:simpleContent >< xs:annotation >< xs:documentation >The base type is the name of the dictionary file.< xs:extension base ="xs:string">

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

    As you can see, we've made the element definitions and type definitions section more complete here.

    Note that we have only included a single global element here. A global element corresponds to an xs:elementdefinition that is a child (but not a grandchild etc.) of an xs:schema element. This is because any element defined by aglobal element is allowed to be the top-level element in a valid XML instance. Normally it is desired to have only one

    possible top-level element in an XML instance, and so this constraint is imposed in the schema by only having oneglobal element definition.

    [Top] [Contents]

    13 - Simple Steps to Writing a SchemaCreating your first schema can be a bit daunting. It helps to have a schema-aware editor, but any text editor will suffice.If you have access to it, the text mode of the XSD Schema editor included as part of Microsoft Visual Studio shouldsuffice for much of your initial needs. The Intellisense of this schema editor is schema aware, and this can help you get avalid schema.

    Having selected an editor, follow these steps to create your first schema:

    1. If the outer most element of your XML instance is to be a sequence (as described in 8.2 - Defining Types thatContain Multiple Elements ), copy the following into your editor:

    < xs:schema targetNamespace ="http://MyDomain.com/MySchema.xsd" xmlns ="http://MyDomain.com/MySchema.xsd" xmlns:xs ="http://www.w3.org/2001/XMLSchema" elementFormDefault ="qualified" >

    < xs:element name ="MyGlobalElement" type ="MyGlobalElementType"/>

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 18/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    19/20

    < xs:complexType name ="MyGlobalElementType">< xs:sequence >

    < xs:element name ="MyFirstElement"type ="...TODO: add the type..."/>

    2. If the outer most element of your XML instance is to be a choice (as described in 8.3 - Defining Types thatContain One of a Selection of Elements ), copy the following into your editor:

    < xs:schema targetNamespace ="http://MyDomain.com/MySchema.xsd" xmlns ="http://MyDomain.com/MySchema.xsd" xmlns:xs ="http://www.w3.org/2001/XMLSchema"

    elementFormDefault ="qualified" >

    < xs:element name ="MyGlobalElement" type ="MyGlobalElementType"/>

    < xs:complexType name ="MyGlobalElementType">< xs:choice >

    < xs:element name ="MyFirstElement"type ="...TODO: add the type..."/>

    3. Customize the names beginning with 'My...' to your needs.4. Add additional element definitions to the ' MyGlobalElementType ' as required.5. Add additional types to the referenced types section as required.6. Save the schema to a file.7. Compile the schema using LMX.8. Integrate the generated code into your project!

    [Top] [Contents]

    14 - Experimenting with SchemasOne of the best ways to learn something is to play around with it. For this reason you may find the LMX evaluationsupport suite a useful tool to help you learn more about XML Schema. This tool contains a number of example schemasand corresponding XML instances. You can also modify the schemas and instances to see how this affects the results.

    9/5/2008 A Simple Overview of W3C XML Schema

    http:/ /www.codalogic.com/lmx/xsd-ov 19/20

  • 8/2/2019 A Simple Overview of W3C XML Schema

    20/20

    The LMX evaluation support suite is included as part of the main LMX XML to C++ code generator download (whichit needs to operate). This can be downloaded from:

    http://codalogic.com/lmx/download.html .

    The suite operates by converting the various schema into C++ code and using the XML instances as input to the programs generated from the C++ code.

    To use the suite fully you will need access to a C++ compiler. To interface to the C++ compiler correctly, you mayneed to modify some of the batch files that the evaluation support suite uses as described in the section titled "BeforeYou Run the Evaluation" in the tools help file.

    [Top] [Contents]

    15 - Things We've Not CoveredWe have so far presented the basics of XML Schema. It is hoped that the subset chosen will serve the bulk of your

    initial schema requirements. We also hope that by first getting familiar with the techniques described above, you willhave a strong foundation for understanding the more advanced aspects of XML Schema.

    In case your wondering, what we haven't covered includes using xs:import to import elements from other namespaces(using ref= ), xs:include to include one schema within another, xs:redefine to redefine types from one schema to another,extending and restricting complex types, specifying mixed content, xs:union, xs:list, anonymous complex types, groupsand attributeGroups, substitution groups, abstract elements and abstract types; to name a few.

    [Top] [Contents]

    16 - Please Rate This Article

    Awful, Poor, OK, Good Very HelpfulCheck if you would like future articles in this series

    Submit Rating...

    Advert:

    Back to the LMX Support Page

    Bookmark with:

    Delicious Digg reddit Facebook StumbleUpon

    9/5/2008 A Simple Overview of W3C XML Schema