Building the Root XML Schema Document


Because you already know how to work with XML and understand the XML syntax, you may find that creating the XML Schema document is a simple process. An XML Schema document is an XML file that has an .xsd file extension. The next section enables you to create your first XML Schema document and then associate it to any XML documents. These sections look to detail how to create your root element, other elements, simple types, complex types and more.

The XML Declaration

When you create an XML Schema (just as when you create XML documents), you are required to include an XML declaration. This means that your XML Schema starts with the following XML declaration:

      <?xml version="1.0" ?> 

This is the minimum requirement for the XML declaration, although you can also specify the encoding and whether the XML document is a standalone document.

      <?xml version="1.0" encoding="UTF-8" ?> 

The Root Element

The root element of the XML Schema document is the <xs:schema> element. You might also see schema documents written with the xsd: namespace prefix as well-<xsd:schema>. In addition to the element declaration, you include a namespace declaration.

      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">         <!-- The element and attribute definitions of our document go here -->      </xs:schema> 

The xmlns attribute allows you to specify the prefix and associate it with the W3C's XML Schema namespace http://www.w3.org/2001/XMLSchema. Because xs follows the namespace, this is the item you use preceding all the XML elements of the document. The next sections review some of the other possible attributes of this root element.

attributeFormDefault Attribute

The schema root element can take a series of attributes that enable you to specify some additional behaviors surrounding the use of XML Schema document. The first attribute is the attributeFormDefault attribute. From your schema, you can require that XML elements of the instance document prefix their attributes with some characters associated with a particular namespace. Listing 6-2 shows an example of an XML Schema using this attribute.

Listing 6-2: Using the attributeFormDefault attribute in your XML Schema document

image from book
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:xs="http://www.w3.org/2001/XMLSchema"       xmlns:be="http://www.lipperweb.com/namespace"       targetNamespace="http://www.lipperweb.com/namespace"       attributeFormDefault="qualified">        <xs:element name="Process">          <xs:complexType>                  <xs:sequence>              <xs:element name="Name">                <xs:complexType>                  <xs:simpleContent>                    <xs:extension base="xs:string">                      <xs:attribute name="salutation" type="xs:string" use="required" />                    </xs:extension>                  </xs:simpleContent>                </xs:complexType>              </xs:element>              <xs:element name="Address" type="xs:string" />              <xs:element name="City" type="xs:string" />              <xs:element name="State" type="xs:string" />              <xs:element name="Country" type="xs:string" />              <xs:element name="Order">                <xs:complexType>                  <xs:sequence>                    <xs:element name="Item" type="xs:string" />                    <xs:element name="Quantity" type="xs:int" />                  </xs:sequence>                </xs:complexType>              </xs:element>            </xs:sequence>          </xs:complexType>        </xs:element>      </xs:schema> 
image from book

The attributeFormDefault can take one of two possible values-qualified or unqualified. The default value is unqualified and means that you don't have to qualify the attribute by adding the prefix. Because the attributeFormDefault in this case is set to qualified, you must construct the consuming instance document as presented in Listing 6-3.

Listing 6-3: An instance document

image from book
      <?xml version="1.0" encoding="UTF-8"?>      <be:Process xmlns:be="http://www.lipperweb.com/namespace"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.lipperweb.com/namespace C:\mySchema.xsd">        <Name be:salutation="Mr">Bill Evjen</Name>        <Address>123 Main Street</Address>        <City>Saint Charles</City>        <State>Missouri</State>        <Country>USA</Country>        <Order>           <Item>52-inch Plasma</Item>           <Quantity>1</Quantity>        </Order>      </be:Process> 
image from book

In this case, because the attributeFormDefault attribute in the XML Schema document, mySchema.xsd, is set to qualified, the attribute contained within the <Name> element makes use of the set prefix.

      <Name be:salutation="Mr">Bill Evjen</Name> 

elementFormDefault Attribute

In addition to setting a required prefix for attributes, you can also take the same approach for any elements utilizing the schema. This is presented in Listing 6-4.

Listing 6-4: Using the elementFormDefault attribute in an XML Schema document

image from book
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:xs="http://www.w3.org/2001/XMLSchema"       xmlns:be="http://www.lipperweb.com/namespace"       targetNamespace="http://www.lipperweb.com/namespace"       attributeFormDefault="unqualified"       elementFormDefault="qualified">         <!-- Removed for clarity -->      </xs:schema> 
image from book

Like the attributeFormDefault attribute, the elementFormAttribute element can take two possible values-qualified and unqualified. The default value is unqualified. Using a value of qualified produces instance documents like the one presented in Listing 6-5.

Listing 6-5: An instance document

image from book
      <?xml version="1.0" encoding="UTF-8"?>      <be:Process xmlns:be="http://www.lipperweb.com/namespace"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.lipperweb.com/namespace C:\mySchema.xsd">        <be:Name salutation="Mr">Bill Evjen</be:Name>        <be:Address>123 Main Street</be:Address>        <be:City>Saint Charles</be:City>        <be:State>Missouri</be:State>        <be:Country>USA</be:Country>        <be:Order>           <be:Item>52-inch Plasma</be:Item>           <be:Quantity>1</be:Quantity>        </be:Order>      </be:Process> 
image from book

Using prefixes on elements or even attributes informs the consumer whether these items are in the target namespace or not.

targetNamespace Attribute

As you learn in this book, namespaces are an important part of XML. You can assign your intended namespace to the validation process of the XML document by using the targetNamespace attribute within the <schema> element. This is presented in Listing 6-6.

Listing 6-6: Using the targetNamespace attribute in your XML Schema document

image from book
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:xs="http://www.w3.org/2001/XMLSchema"             xmlns:be="http://www.lipperweb.com/namespace"       targetNamespace="http://www.lipperweb.com/namespace"       attributeFormDefault="unqualified"       elementFormDefault="qualified">         <!-- Removed for clarity -->      </xs:schema> 
image from book

The version Attribute

The version attribute allows you to easily place a signifier of the version of the XML Schema document directly within the root element. It is important to note that the version attribute has nothing to do with the version of the W3C XML Schema Language which is used, but instead it is the version of the schema document itself. This is shown in Listing 6-7.

Listing 6-7: Using the version attribute in your XML Schema document

image from book
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:xs="http://www.w3.org/2001/XMLSchema"       xmlns:be="http://www.lipperweb.com/namespace"       targetNamespace="http://www.lipperweb.com/namespace"       attributeFormDefault="unqualified"       elementFormDefault="qualified"       version="1.3">         <!-- Removed for clarity -->      </xs:schema> 
image from book

xml:lang Attribute

The xml:lang attribute allows you to signify the language that is utilized for the XML Schema document. This useful if you have versions of the same XML Schema document that are different only because of the element and attribute names that are used. An example of this attribute is presented in Listing 6-8.

Listing 6-8: Using the xml:lang attribute in the XML Schema document

image from book
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:xs="http://www.w3.org/2001/XMLSchema"       xmlns:be="http://www.lipperweb.com/namespace"       targetNamespace="http://www.lipperweb.com/namespace"       attributeFormDefault="unqualified"       elementFormDefault="qualified"       xml:lang="en-US">         <!-- Removed for clarity -->      </xs:schema> 
image from book

From this example, you can see that the schema is defined as being of language en-US, which is a specific culture signifying English as spoken in the United States. Specifying the xml:lang attribute with en-GB specifies that the document is for English as spoken in the United Kingdom and fi-FI signifies that the document is for Finnish as spoken in Finland.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net