Altova XMLSpy 2024 Enterprise Edition

This example illustrates how to work with program code generated from a "main" XML schema that imports other schemas. Each of the imported schema has a different target namespace. The goal here is to create programmatically an XML document where all elements are prefixed according to their namespace. More specifically, the XML document created from your C++, C#, or Java code should look like the one below:

 

<?xml version="1.0" encoding="utf-8"?>
<p:Purchase xsi:schemaLocation="http://NamespaceTest.com/Purchase Main.xsd"
          xmlns:p="http://NamespaceTest.com/Purchase"
          xmlns:o="http://NamespaceTest.com/OrderTypes"
          xmlns:c="http://NamespaceTest.com/CustomerTypes"
          xmlns:cmn="http://NamespaceTest.com/CommonTypes"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <p:OrderDetail>
      <o:Item>
          <o:ProductName>Lawnmower</o:ProductName>
          <o:Quantity>1</o:Quantity>
          <o:UnitPrice>148.42</o:UnitPrice>
      </o:Item>
  </p:OrderDetail>
  <p:PaymentMethod>VISA</p:PaymentMethod>
  <p:CustomerDetails>
      <c:Name>Alice Smith</c:Name>
      <c:DeliveryAddress>
          <cmn:Line1>123 Maple Street</cmn:Line1>
          <cmn:Line2>Mill Valley</cmn:Line2>
      </c:DeliveryAddress>
      <c:BillingAddress>
          <cmn:Line1>8 Oak Avenue</cmn:Line1>
          <cmn:Line2>Old Town</cmn:Line2>
      </c:BillingAddress>
  </p:CustomerDetails>
</p:Purchase>

 

The main schema used in this example is called Main.xsd. As illustrated in the code listing below, it imports three other schemas: CommonTypes.xsd, CustomerTypes.xsd, and OrderTypes.xsd. To get the same results as in this example, save all the code listings below to files, and use the same file names as above. Notice that the schema maps each of the prefixes ord, pur, cmn, and cust to some namespace (Order types, Purchase types, Common types, and Customer types, respectively). This means that, in the generated code, the classes corresponding to Orders, Purchases, Customers, and so on, will be available under their respective namespace.

 

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://NamespaceTest.com/Purchase"
    xmlns:ord="http://NamespaceTest.com/OrderTypes"
    xmlns:pur="http://NamespaceTest.com/Purchase"
    xmlns:cmn="http://NamespaceTest.com/CommonTypes"
    xmlns:cust="http://NamespaceTest.com/CustomerTypes"
    elementFormDefault="qualified">
  <xs:import schemaLocation="CommonTypes.xsd" namespace="http://NamespaceTest.com/CommonTypes" />
  <xs:import schemaLocation="CustomerTypes.xsd" namespace="http://NamespaceTest.com/CustomerTypes" />
  <xs:import schemaLocation="OrderTypes.xsd" namespace="http://NamespaceTest.com/OrderTypes" />
  <xs:element name="Purchase">
      <xs:complexType>
          <xs:sequence>
              <xs:element name="OrderDetail" type="ord:OrderType" />
              <xs:element name="PaymentMethod" type="cmn:PaymentMethodType" />
              <xs:element ref="pur:CustomerDetails" />
          </xs:sequence>
      </xs:complexType>
  </xs:element>
  <xs:element name="CustomerDetails" type="cust:CustomerType" />
</xs:schema>

Main.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://NamespaceTest.com/CommonTypes"
    elementFormDefault="qualified">
  <xs:complexType name="AddressType">
    <xs:sequence>
        <xs:element name="Line1" type="xs:string"/>
        <xs:element name="Line2" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <xs:simpleType name="PriceType">
    <xs:restriction base="xs:decimal">
        <xs:fractionDigits value="2"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="PaymentMethodType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="VISA"/>
        <xs:enumeration value="MasterCard"/>
        <xs:enumeration value="Cash"/>
        <xs:enumeration value="AMEX"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

CommonTypes.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://NamespaceTest.com/CustomerTypes"
    xmlns:cmn="http://NamespaceTest.com/CommonTypes"
    elementFormDefault="qualified">
  <xs:import schemaLocation="CommonTypes.xsd" namespace="http://NamespaceTest.com/CommonTypes" />
  <xs:complexType name="CustomerType">
      <xs:sequence>
          <xs:element name="Name" type="xs:string" />
          <xs:element name="DeliveryAddress" type="cmn:AddressType" />
          <xs:element name="BillingAddress" type="cmn:AddressType" />
      </xs:sequence>
  </xs:complexType>
</xs:schema>

CustomerTypes.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://NamespaceTest.com/OrderTypes"
    xmlns:cmn="http://NamespaceTest.com/CommonTypes"
    elementFormDefault="qualified">
  <xs:import schemaLocation="CommonTypes.xsd" namespace="http://NamespaceTest.com/CommonTypes" />
  <xs:complexType name="OrderType">
      <xs:sequence>
          <xs:element maxOccurs="unbounded" name="Item">
              <xs:complexType>
                  <xs:sequence>
                      <xs:element name="ProductName" type="xs:string" />
                      <xs:element name="Quantity" type="xs:int" />
                      <xs:element name="UnitPrice" type="cmn:PriceType" />
                  </xs:sequence>
              </xs:complexType>
          </xs:element>
      </xs:sequence>
  </xs:complexType>
</xs:schema>

OrderTypes.xsd

To complete this example, take the following steps:

 

1.Save all schemas from the code listings above to files on the disk, making sure that you preserve the indicated file names.

2.Generate the schema wrapper code from the Main.xsd schema above, using the steps described in Generating Code from XML Schemas or DTD. After completing this step, you should have generated a compilable program in the language of your choice (C++, C#, or Java).

3.Add code to your C++, C#, or Java program from one the following example code listings, as required:

 

XML Namespaces and Prefixes (C++)

XML Namespaces and Prefixes (C#)

XML Namespaces and Prefixes (Java)

© 2017-2023 Altova GmbH