Datypic Logo
Definitive XML Schema

Definitive XML Schema

Priscilla Walmsley (pwalmsley@datypic.com)

1st edition (December 7, 2001)

Prentice Hall PTR; ISBN: 0130655678

Chapter 20: Naming considerations

Book examples

Example 20-1. Repetition of terms
<product>
  <prodNumber>557</prodNumber>
  <prodName>Short-Sleeved Linen Blouse</prodName>
  <prodSize sizeSystem="US-DRESS">10</prodSize>
</product>
Example 20-2. No repetition of terms
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <size system="US-DRESS">10</size>
</product>
Example 20-3. Less clear context
<letter>Dear <custName>Priscilla Walmsley</custName>,
Unfortunately, we are out of stock of the
<prodName>Short-Sleeved Linen Blouse</prodName> in size
<prodSize>10</prodSize> that you ordered...</letter>
Example 20-4. Qualified local names
<ord:order xmlns:ord="http://example.org/ord"
           xmlns:prod="http://example.org/prod">
  <ord:number>123ABBCC123</ord:number>
  <ord:items>
    <prod:product>
      <prod:number>557</prod:number>
    </prod:product>
  </ord:items>
</ord:order>
Example 20-5. Unqualified local names
<ord:order xmlns:ord="http://example.org/ord">
  <number>123ABBCC123</number>
  <items>
    <product>
      <number>557</number>
    </product>
  </items>
</ord:order>
Example 20-6. Schema for qualified local element-type names
ord.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:prod="http://example.org/prod"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord"
            elementFormDefault="qualified">
  <xsd:import schemaLocation="prod.xsd"
              namespace="http://example.org/prod"/>
  <xsd:element name="order" type="OrderType"/>
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="number" type="OrderNumType"/>
      <xsd:element name="items" type="prod:ItemsType"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
prod.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod"
            elementFormDefault="qualified">
  <xsd:complexType name="ItemsType">
    <xsd:sequence maxOccurs="unbounded">
      <xsd:element name="product" type="ProductType"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
Example 20-7. Invalid mixing of unqualified names and default namespace
<order xmlns="http://example.org/ord">
  <number>123ABBCC123</number>
  <items>
    <product>
      <number>557</number>
    </product>
  </items>
</order>
Example 20-8. Qualified and unqualified attribute names
Schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xsd:attribute name="global" type="xsd:string"/>
  <xsd:element name="size" type="SizeType"/>
  <xsd:complexType name="SizeType">
    <xsd:attribute ref="global"/>
    <xsd:attribute name="unqual" form="unqualified"/>
    <xsd:attribute name="qual" form="qualified"/>
    <xsd:attribute name="unspec"/>
  </xsd:complexType>
</xsd:schema>
Valid instance:
<prod:size prod:global="x" unqual="x" prod:qual="x" unspec="x"/>
Example 20-9. Same namespace in a schema
ord.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/all"
            targetNamespace="http://example.org/all"
            elementFormDefault="qualified">
  <xsd:include schemaLocation="prod.xsd"/>
  <xsd:include schemaLocation="cust.xsd"/>
  <xsd:element name="order" type="OrderType"/>
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="customer" type="CustomerType"/>
      <xsd:element name="items" type="ItemsType"/>
    </xsd:sequence>
  </xsd:complexType>
  <!--...-->
</xsd:schema>
prod.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/all"
            targetNamespace="http://example.org/all"
            elementFormDefault="qualified">
  <xsd:complexType name="ItemsType">
    <xsd:sequence maxOccurs="unbounded">
      <xsd:element name="product" type="ProductType"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
cust.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/all"
            targetNamespace="http://example.org/all"
            elementFormDefault="qualified">
  <xsd:complexType name="CustomerType">
    <xsd:sequence>
      <xsd:element name="name" type="CustNameType"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:simpleType name="CustNameType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>
</xsd:schema>
Example 20-10. Same namespace in an instance
<order xmlns="http://example.org/all"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://example.org/all ord.xsd">
  <customer>
    <name>Priscilla Walmsley</name>
  </customer>
  <items>
    <product>
      <number>557</number>
    </product>
  </items>
</order>
Example 20-11. Different namespaces in a schema
ord.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:prod="http://example.org/prod"
            xmlns:cust="http://example.org/cust"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord"
            elementFormDefault="qualified">
  <xsd:import schemaLocation="prod.xsd"
              namespace="http://example.org/prod"/>
  <xsd:import schemaLocation="cust.xsd"
              namespace="http://example.org/cust"/>
  <xsd:element name="order" type="OrderType"/>
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="customer" type="cust:CustomerType"/>
      <xsd:element name="items" type="prod:ItemsType"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
prod.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod"
            elementFormDefault="qualified">
  <xsd:complexType name="ItemsType">
    <xsd:sequence maxOccurs="unbounded">
      <xsd:element name="product" type="ProductType"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
cust.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/cust"
            targetNamespace="http://example.org/cust"
            elementFormDefault="qualified">
  <xsd:complexType name="CustomerType">
    <xsd:sequence>
      <xsd:element name="name" type="CustNameType"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:simpleType name="CustNameType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>
</xsd:schema>
Example 20-12. Different namespaces in an instance
<order xmlns="http://example.org/ord"
       xmlns:prod="http://example.org/prod"
       xmlns:cust="http://example.org/cust"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://example.org/ord ord.xsd">
  <customer>
    <cust:name>Priscilla Walmsley</cust:name>
  </customer>
  <items>
    <prod:product>
      <prod:number>557</prod:number>
    </prod:product>
  </items>
</order>
Example 20-13. Different namespaces in an instance, with default namespaces
<order xmlns="http://example.org/ord"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://example.org/ord ord.xsd">
  <customer>
    <name xmlns="http://example.org/cust">Priscilla Walmsley</name>
  </customer>
  <items>
    <product xmlns="http://example.org/prod">
      <number>557</number>
    </product>
  </items>
</order>
Example 20-14. Chameleon namespaces in a schema
ord.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord"
            elementFormDefault="qualified">
  <xsd:include schemaLocation="prod.xsd"/>
  <xsd:include schemaLocation="cust.xsd"/>
  <xsd:element name="order" type="OrderType"/>
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:string"/>
      <xsd:element name="items" type="ItemsType"/>
    </xsd:sequence>
  </xsd:complexType>
  <!--...-->
</xsd:schema>
prod.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified">
  <xsd:complexType name="ItemsType">
    <xsd:sequence maxOccurs="unbounded">
      <xsd:element name="product" type="ProductType"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
cust.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified">
  <xsd:complexType name="CustomerType">
    <xsd:sequence>
      <xsd:element name="name" type="CustNameType"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:simpleType name="CustNameType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>
</xsd:schema>
Example 20-15. English-language element-type names
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element ref="number"/>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element ref="size"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="SizeType">
    <xsd:simpleContent>
      <xsd:extension base="xsd:integer">
        <xsd:attribute name="system" type="xsd:string"/>
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>
  <xsd:element name="product" type="ProductType"/>
  <xsd:element name="number" type="xsd:string"/>
  <xsd:element name="size" type="SizeType"/>
</xsd:schema>
Example 20-16. Using substitution groups for localization
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="produit" substitutionGroup="product"/>
  <xsd:element name="numéro" substitutionGroup="number"/>
  <xsd:element name="taille" substitutionGroup="size"/>
</xsd:schema>
Example 20-17. Localized instance
<produit>
  <numéro>557</numéro>
  <name>Short-Sleeved Linen Blouse</name>
  <taille system="US-DRESS">10</taille>
</produit>
Datypic XML Schema Services