Putting XML Schema Document Together


You put XML Schema documents together through the use of either the <import> or <include> elements. Using these is something I recommend because it allows you to create reusable schema documents that you can use as a foundation whenever you build an XML Schema document.

<import>

Imports allow you to import another entire XML Schema document into the one that you are already working with. This is usually done if the two varying XML Schema documents utilize different namespaces. You use the import statement as shown in the example in Listing 6-46.

Listing 6-46: Importing another XML Schema document

image from book
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"       xmlns:LipperNamespace="http://www.lipperweb.com/OtherNamespace"       targetNamespace="http://www.lipperweb.com/Namespace"       elementFormDefault="qualified">        <xs:import namespace="http://www.lipperweb.com/OtherNamespace"         schemaLocation="OtherSchema.xsd" />        <xs:element name="Process">          <xs:complexType>            <xs:sequence>              <xs:element name="Name" type="xs:string" />              <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:sequence>          </xs:complexType>        </xs:element>      </xs:schema> 
image from book

This import is done using the <import> element and two attributes-namespace and schemaLocation. When doing so, you also want to associate the namespace with a prefix within the <schema> element.

      xmlns:LipperNamespace="http://www.lipperweb.com/OtherNamespace" 

This allows you to use the items declared in either XML Schema document. If you are referencing an item that is contained in the other schema, you use a construction like the one shown here:

      <LipperNamespace:OtherElement>value</LipperNamespace:OtherElement> 

<include>

The <include> element is used if the other schema has the same namespace or makes use of no name-space. It is quite similar to the <import> element and is a good way to combine two schemas with little work. Listing 6-47 shows an example of using the <include> element.

Listing 6-47: Importing another XML Schema document

image from book
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"       targetNamespace="http://www.lipperweb.com/Namespace"       elementFormDefault="qualified">        <xs:include schemaLocation="OtherSchema.xsd" />        <xs:element name="Process">          <xs:complexType>            <xs:sequence>              <xs:element name="Name" type="xs:string" />              <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:sequence>          </xs:complexType>        </xs:element>      </xs:schema> 
image from book




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