5

I have an XSD package composed of two XSD files: let's say A.xsd and B.xsd. The schema A imports (not includes) the B schema. They have different namespaces.

I need to use those schema in a system that doesn't work well with xsd:imports.

Is it possible to flatten an XSD package in one and only one XSD file or is one XSD file per namespace the absolute minimum?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Georges Martin
  • 1,158
  • 1
  • 8
  • 16

2 Answers2

2

Each xs:schema element contains declarations for just one target namespace. But nothing in the spec says that xs:schema has to be the root element of the XML document. In theory, therefore, you could just wrap both schema elements in a wrapper file; in practice, not all XSD validators will be happy with that as input.

[Later addition: since it seems to be a contentious point, perhaps I should say explicitly that I'm assuming that by "one XSD file" you mean one XML document, contained in a single file, containing the XSD. If you mean something else, please explain.]

You don't say what kind of problems you're having with imports, but one thing that may be worth trying is to remove the schema location information from the xs:import element in A.xsd (so the import just names the namespace involved, not the location of the schema document), and pass both A.xsd and B.xsd to the processor as run-time parameters. (Most though not all XSD processors allow you to specify at invocation time some schema documents for them to read. The order of the arguments can matter, so if it doesn't work the first time, try reversing the order.)

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • Please point me to one single implementation that is an XSD file and has two or more xs:schema elements and I will be happy to revert my downvote. I think you had in mind the comment at the bottom of section 3.15.2. Keep in mind though that the question refers to **XSD files**. A WSDL file can embedded multiple schema documents targetting different namespaces. However, that is not an XSD file. It is a particular type of document that supports the embedding of XML Schema documents. – Petru Gardea Sep 21 '12 at 01:58
  • You seem to be conflating what is possible in conforming software and data with what is currently supported by vendors; I think it is more helpful to keep those two things distinct, as indeed I did in my answer. Claims about what is said by a spec are not made true or false by observations about the behavior of processors. – C. M. Sperberg-McQueen Sep 21 '12 at 16:37
  • It's a side issue, I think, but since you ask, [XSV](http://www.w3.org/2001/03/webdata/xsv) appears to support multiple xs:schema elements in the same XML document. – C. M. Sperberg-McQueen Sep 21 '12 at 19:07
  • I don’t think that is what the question is about; it refers to `a system` that deals with **XSD files**. I clearly said, `there's only one root xs:schema per XSD`; I **did not** say, `there's only one xs:schema per WSDL` which would be "not quite true". You rather appear to disagree with the definition of `XSD files`... which is cool, but rather beside the point here. – Petru Gardea Sep 21 '12 at 22:08
  • Thank you for the reference - I wouldn't say its working. Did you try it? I've tried `` and got `` - I would've expected an error. Tried a good schema wrapped with a dummy... – Petru Gardea Sep 21 '12 at 22:24
  • (cont'd) root element, or a good schema without wrapper, it reports "all's well". I passed in XML fragments (basically no root, just siblings), or plain text, it says the same. Sounds like is not functioning - at least at this time. Anyway, maybe I'm doing something wrong... – Petru Gardea Sep 21 '12 at 22:30
  • Yes, maybe you are doing something wrong. This is probably not the right place for a detailed discussion of XSV runtime options or how to interpret XSV output, though, so I've put a description of how to demonstrate this behavior in XSV on a separate Web site (at http://blackmesatech.com/2012/09/xsv-demo). – C. M. Sperberg-McQueen Sep 22 '12 at 18:05
  • I agree on XSV not being the subject of this (btw, it is the uploading of files that doesn't seem to be working). I noticed though that you avoided the use of "XSD file" throughout; you concluded `It thus illustrates that multiple spec-conformant schema documents can be embedded in the same XML document.`. What would you call that **XML document**, when serialized as a file? An XSV file? The point still stands - it is not an XSD file. My own application supports the embedding of WSDL and XSD files in one "cache" file. While I can use this cache to run WS-I tests or XSD validation, the cache... – Petru Gardea Sep 23 '12 at 13:48
  • (cont'd) itself is neither an WSDL, nor an XSD file. It is a proprietary format specific to my application. I am not stating that what people commonly refer to as an XSD file is a standard file format endorsed by W3C. I would argue though that it is a **de facto standard** in the industry. Regardless of this disagrement, I appreciate all that you've shown, and the excelent precision you demonstrated in your discourse. I'll end here this argument. – Petru Gardea Sep 23 '12 at 13:56
  • It's a file, and the only thing it contains is a vacuous wrapper element and XSD declarations; yes, of course I'd call it an XSD file. Wouldn't you? (If "XSD file" is taken to mean "an XML document rooted in an xs:schema element", then the OP's question becomes vacuous, the answer is trivially "no" but has nothing to do with XSD but only with XML well-formedness. It seems better to give the OP the benefit of the doubt and assume they were asking a real question, not a trivial or stupid one.) – C. M. Sperberg-McQueen Sep 24 '12 at 17:30
1

No, it is not possible. You can only target one namespace per XSD. If anything, there's only one root xs:schema per XSD, with only one attribute (which is not a list) called targetNamespace.

Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • Indeed... I re-read some XSD standard docs and realized that. Thanks for the confirmation :-) – Georges Martin Sep 20 '12 at 15:26
  • 1
    Not quite true. One namespace per "schema document", but the XSD spec defines "schema document" as `xs:schema` element. It's called a document because `xs:schema` is usually the outermost element, but that's not actually a requirement of the spec. – C. M. Sperberg-McQueen Sep 21 '12 at 00:21