MGBrown.com Hunk

MGBlog

Wednesday, 18 August 2004

Wed, 18 Aug 2004 - 11:22pm (GMT+1)
Comments

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 &lt;xs:import&gt; or &lt;xs:include&gt; and xsd.exe

Tried using your example, there seems to be a <xs:schema tag missing in the second schema.

Re: Using &lt;xs:import&gt; or &lt;xs:include&gt; and xsd.exe

You were quite right. I forgot to escape the < to &lt;

Updated.

Re: Using &lt;xs:import&gt; or &lt;xs:include&gt; and xsd.exe

is there a way not to generate types for the included schemas. ?

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 <xs:import>. When using <xs:include> it takes account of the schemaLocation.

Re: Using import or include with xsd.exe

Thanks! This worked for me.

Re: Using import or include with xsd.exe


FAB, been trying to get this to work all morning THANKS!

Add Your Comment

Your Name:
Harry Potter
Your homepage URL (optional):  
http://www.mgbrown.com
Title:*
Comment:*
Copy code into box:
 
* HTML tags not supported