Scott Hanselman

XSD.exe is kicking my butt, man...redux

April 25, '03 Comments [5] Posted in Web Services | XML | Tools
Sponsored By

Decomposing XSD and WSDL into abstract definitions of types and messages is a good thing, but it appears that Microsoft’s tools don’t adequately support embracing this approach? Or, I'm completely sans clue.  For example, XSD.exe doesn’t seem to be able to cope with xs:import elements when generating wrapper classes.  WSDL.exe has the same problem. 

Anyone have any thoughts on this?  I'd like NOT to wait until Whidbey to be able to describe my messages and contracts and generate a schtickel of code.  Updated with Note: Rant retracted...for now! See below ;)

Here’s a very trivial example straight from Dare's great MSDN Article.

Import.xsd:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema 
targetNamespace="http://www.import.org" 
elementFormDefault="qualified"> 
      <xs:complexType name="rootType" >  
            <xs:sequence>
                  <xs:element name="child1" type="xs:string" maxOccurs="2" />
                  <xs:element name="child2" type="xs:string"/>
            </xs:sequence>
      </xs:complexType>
</xs:schema>

The Outer xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema
 targetNamespace="http://www.example.org
 xmlns:tns="http://www.example.org"
 xmlns:imp="http://www.import.org">
<xs:import namespace="http://www.import.org" schemaLocation="file:///c:/import.xsd"/>
 <xs:element name="root" type="imp:rootType" />
</xs:schema>

This schema appears to be valid – in fact, the .NET framework agrees.  But when you try and run the outer schema through xsd.exe, you get the following error:

C:\>xsd /c root.xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 1.1.4322.573]
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Schema validation warning: Type 'http://www.import.org:rootType' is not declared
. An error occurred at file:///C:/root.xsd, (10, 3).

Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.

Error: Error generating classes for schema 'root'.
  - The datatype 'http://www.import.org:rootType' is missing.

Update: Tomas was kind enough to point out not only that I had originally posted the same schema TWICE, doh!  But also, more importantly, that XSD.EXE doesn't resolve schemaLocation on imports or includes, presuambly because the W3C Schema spec describes the schemaLocation attribute as a hint, not a true location.

I'm back in business as I ran XSD.EXE again with all necessary files specified on the commandline, in order to give XSD.EXE a choice of namespaces - as opposed to hoping it would somehow glean my intent!

I'm too used to using XMLSpy, XSLT and other tools for generating CS code.  Good stuff, thanks Tomas!  Not only was I without a clue, I was also sin una pistasans indice - Ich war ohne einen Anhaltspunkt.  That'll teach me not to post a rant in haste! 

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Saturday, April 26, 2003 12:46:08 AM UTC
Scott: Is it me, or did you post twice the same XSD snippet?

Saturday, April 26, 2003 7:51:43 AM UTC
I posted once, then corrected an error in categories and posted again...

Saturday, April 26, 2003 7:57:58 AM UTC
You know, it's 1am, but the more I look at this the more I realize that Tomas is probably right and I'm even wondering why I even thought it could work the way I was doing it. Of course! How could it infer where the schema was unless I tell it...we shall see when I wake up. Gracias Tomas.
Saturday, April 26, 2003 8:05:31 AM UTC
Wow, I AM falling apart...I posted the same schema twice...fixing that, then checking my temperature! :)
Friday, May 21, 2004 8:41:09 PM UTC
Hi,

I am struggling with a similiar issue. You said that you have to explicitely tell XSD.exe the schema location. Can you please give an example of the command line syntax> Also, will fully qualified path in xs:import resolve this issue?

Thanks,
Anand
Anand Desai
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.