MGBrown.com

Using import or include with xsd.exe

I have recently been playing around with XML schemas and creating class models from them using xsd.exe (see MSDN).

I ran into a stumbling block, however, when I tried to use it to generate classes from a schema that used the <xsd:import> statement to import another schema. Although it is not very obvious from the documentation, it does support this, however it does not support the schemaLocation hint.

In order to use schemas that import or include other schemas you need to specify both the base schema and the imported schema on the command line.

For example if you have the following two schemas:

Schema1

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://example.com/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:examp1="http://example.com/XMLSchema1.xsd"
    xmlns:examp2="http://example.com/XMLSchema2.xsd">

    <xs:import namespace="http://example.com/XMLSchema2.xsd" schemaLocation="XMLSchema2.xsd" />

   <xs:element name="example">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="test" type="examp2:myType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Schema2

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://example.com/XMLSchema2.xsd"
    xmlns:examp2="http://example.com/XMLSchema2.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="myType">
        <xs:sequence>
            <xs:element name="test" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

You would need to use the following command line:

xsd.exe schema1.xsd schema2.xsd /c

Comments

Re: Using import or include with xsd.exe

Useful tip about adding name of included schema in xsd.exe command line. However, this only seems to be necessary with &lt;xs:import&gt;. When using &lt;xs:include&gt; it takes account of the schemaLocation.

Comment from Tim at Thursday, 30 March 2006 09:33AM (GMT+00)

Sorry, this post is no longer accepting comments.