XSD Schemas for VB Developers

Schemas and Namespaces

If you are building Web services or using XML for data interchange between your business and other businesses, you’ll need to use XML namespaces to fully qualify your elements and your data types (see the XML namespaces tutorial on this site). For example, if you are using the namespace http://schemas.devxpert.com/order, your order document might look like this:

<Order xmlns="http://schemas.devxpert.com/order">
  <OrderId>2093324</OrderId>
  <RequiredDate>2001-08-01</RequiredDate>
  <ShipName>Yasser Shohoud</ShipName>
  <OrderDetails>
    <OrderItem>
      <ProductID>53034</ProductID>
      <Quantity>9</Quantity>
      <UnitPrice>12.09</UnitPrice>
    </OrderItem>
    <OrderItem>
      <ProductID>66090</ProductID>
      <Quantity>12</Quantity>
      <UnitPrice>10.09</UnitPrice>
    </OrderItem>
    <OrderItem>
      <ProductID>56091</ProductID>
      <Quantity>2</Quantity>
      <UnitPrice>87.95</UnitPrice>
    </OrderItem>
  </OrderDetails>
</Order>

In your schema, you specify the targetNamespace attribute on the <xsd:schema> element.

<schema targetNamespace="http://schemas.devxpert.com/order" xmlns:dx="http://schemas.devxpert.com/order" xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

Besides defining the targetNamespace, we also define the prefix dx, which maps to the same namespace. We’ll use this prefix shortly. We also defined elementFormDefault=”qualified” which means that elements in the Order XML document will be qualified (either by having a namespace prefix or by using a default namespace like the example above).

All types defined within the schema belong to the target namespace and must be referenced using their fully qualified name, which is the namespace prefix followed by a colon followed by the type name. For example, to reference the OrderItemType you’d write dx:OrderItemType because dx is the prefix that’s declared to map to the target namespace. So the full schema using namespaces would be:

<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.devxpert.com/order"
xmlns:dx="http://schemas.devxpert.com/order"
elementFormDefault="qualified">
  <!-- this is the equivalent of the OrderItem class -->
  <xsd:complexType name="OrderItemType">
    <xsd:sequence>
      <xsd:element name="ProductID" type="xsd:int"/>
      <xsd:element name="Quantity" type="xsd:short"/>
      <xsd:element name="UnitPrice" type="xsd:double"/>
    </xsd:sequence>
  </xsd:complexType>
  <!-- this corresponds to the Order class -->
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="OrderId" type="xsd:int"/>
      <xsd:element name="RequiredDate" type="xsd:date"/>
      <xsd:element name="ShipName" type="xsd:string"/>
      <xsd:element name="OrderDetails" type="dx:OrderDetailsType"/>
    <!-- the following is the equivalent of the OrderDetails collection -->
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="OrderDetailsType">
    <xsd:sequence>
      <xsd:element name="OrderItem"
        type="dx:OrderItemType" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <!-- this corresponds to the Order class -->
  <xsd:element name="Order" type="dx:OrderType"/>
</xsd:schema>

In your XML document, you use the attribute schemaLocation to specify which XSD schema is used to validate elements that belong to a given namespace:

<Order xmlns="http://schemas.devxpert.com/order"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.devxpert.com/order d:\schemas\order.xsd">

The schemaLocation attribute lets you specify a namespace and the corresponding schema. Its like saying “All elements that belong to this namespace must be validated using that schema”. If your document happens to contain elements from different namespaces (as is the case with WSDL documents) you can specify a different schema for each namespace by listing them all in the schemaLocation attribute. This is very powerful because it lets you mix elements from different schemas in the same document while validating each element with the correct schema.

You might have noticed the xsi namespace prefix on the schemaLocation attribute. XSD defines a few attributes that can be used in the XML document itself as opposed to the XSD schema document. These attributes belong to the XML schema instance namespace, which is http://www.w3.org/2001/XMLSchema-instance. By convention, the prefix used for this namespace is usually xsi.

You might also like...

Comments

About the author

Yasser Shohoud United States

Yasser started programming at the age of 12 when he wrote his first text-based game on a Commodore PET. He's since moved to IBM mainframes then to Microsoft technologies and has worked as System...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor