Why does XSD.EXE Create Two .XSD Files, and How to Use Them?
Asked Answered
U

4

7

I am using the following command to generate related xsd file from my input XML file -- response.xml, my confusion is (1) why there are two xsd file output (response.xsd and response_app1.xsd), I think there should be only one of them generated? (2) If I need to generate C# class file by using xsd /classes, which xsd file should I use?

My environment: VSTS 2008 + C# + .Net 3.5.

D:\>xsd response.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'D:\response.xsd'.

Here is my input XML file and ouput XSD files,

http://www.mediafire.com/file/kzzoxw0zwgq/inputoutput.zip

EDIT 1:

when I execute xsd response.xsd to generate related C# class, there are error messages, and here are the detailed output,

D:\>xsd response.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: The 'http://www.mycorp.com/Order/2009/05/mex:Items' e
lement is not declared. Line 10, position 16.

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

Error: Error generating classes for schema 'response'.
  - The element 'http://www.mycorp.com/Order/2009/05/mex:Items' is missing.

If you would like more help, please type "xsd /?".

thanks in advance, George

Unnerve answered 5/7, 2009 at 13:33 Comment(0)
P
6

George,

To generate classes from these files, you must list both on the command line:

D:\>xsd response.xsd response_app1.xsd /classes 

It has already been correctly said that the two .xsd files are because there were two XML namespaces in your original XML file. BTW, I'm guessing a little here because you did not post the XML file.

Phoenix answered 5/7, 2009 at 16:24 Comment(2)
Thanks, Why there are two generated xsd files other than one? And what are the different functions of the two files?Unnerve
BTW: your solution works. Any comments or ideas to my above quesiton? :-)Unnerve
B
2

The reason why two files are getting generated is because your XML contains two XML namespaces; each generated XSD corresponds to a namespace. The one for the soap envelope namespace imports the mycorp namespace (which you can see from the xs:import element).

Become answered 5/7, 2009 at 16:20 Comment(10)
My confusion is, why one XSD file could only contains one namespace?Unnerve
Another confusion is there are 4 namespaces, they are xmlns:soapenv="schemas.xmlsoap.org/soap/envelope" xmlns:xsd="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance" and mycorp namespace, why you said there are only 2 namespaces?Unnerve
While those other namespaces are declared in your XML document, no elements actually use them.Become
In response to your first question, an XML namespace typically maps to and identifies a schema. It makes sense what the XSD tool does. Are you sure you need to generate a schema for the whole thing? The SOAP envelope schema is already defined (w3.org/TR/2000/NOTE-SOAP-20000508/envelope-2000-04-18.xml). You may just want to run XSD on a document containing the SOAP payload instead.Become
Thanks Jacob, 1. I want to confirm with you that you mean only mycorp (default namespace) and soapenv are used in the XML document, the xsi/xsd are not used, so they are ignored in the generated xsd file? 2. I still think generate everything into one schema (.xsd) file is more convenient, why do you think generate separate files are better?Unnerve
1. Yes. 2. Different namespaces typically mean different applications. For example, XHTML can include MATHML through using different namespaces. However, since they exist independently, it wouldn't make sense for an XML Schema Definition file to include both.Become
@Jacob, I read more details into the generated XSD files, and actually they are cross-reference each other, so I am confused about why you said they exist independently? And I am also not sure (since cross reference) whether they could be used independently without the existence of others. Any comments? BTW: cross reference I mean in each xsd file, there is target namespace for defined elements inside the namespace itself, but it contains also reference namespace other the other, which I mean cross reference.Unnerve
What I'm saying is that XML namespaces can exist independently, so they typically reside in separate XSDs. In the case of your generated schema's, they're related, so one is imported into the other. The whole separate XSD thing was simply a file organization choice made by the creator of the tool.Become
"The whole separate XSD thing was simply a file organization choice made by the creator of the tool" -- could you explain your quoted words in some other words please? Sorry my English is not very good. :-)Unnerve
What I mean is that the XSD tool generates two schema files not out of necessity but because it's a common practice to organize schemas.Become
J
1

IIRC, you use both, one will probably reference the other.

Jablonski answered 5/7, 2009 at 13:36 Comment(1)
@leppie, 1. when I execute xsd response.xsd to generate related C# class, there are error messages. I have posted in my original post EDIT 1 section. Any ideas what is wrong? 2. "one will probably reference the other" -- how do you prove this?Unnerve
T
-2

Yes, remove the namespaces from XML file and generate it, you will get single XSD file

Tasia answered 24/9, 2009 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.