10

I frequently use xsd schema files in my work, and I got to wondering: Is it possible to write an xsd schema file for the xsd schema language? It seems like an obvious way to document the schema language, and I would think it would be one of the first things its developers would try. However, I've been unable to find such a file with obvious search terms. If it's not possible, why not? If it is possible, has anyone done it and is there a place I could download such a file?

Notes:

  1. Wikipedia suggests the xsd schema language is "not 100% self describing" because "content and attribute declarations cannot depend on attributes or element context". If this is so, can you provide an example of a feature of xsd that requires such context and therefore can't be represented?

  2. This question isn't purely academic. I may need to write a program that processes xsd schema files in the near future, and generating source code from an xsd (with a tool such as generateDS) would be an easy place to start.

jcrudy
  • 3,921
  • 1
  • 24
  • 31
  • @John I think it probably is, although I did see that one previously and failed to realize the similarity. – jcrudy Jul 19 '13 at 05:26

2 Answers2

7

Indeed, there is an XML schema for the XSD language, which exists from the very start (as its URL suggests):

http://www.w3.org/2001/XMLSchema.xsd

and here's the XSD file for its most recent version -- W3C XML Schema Definition Language (XSD) 1.1:

http://www.w3.org/2012/04/XMLSchema.xsd

In fact, those XSD files (at least the first one) are heavily used now by various software working with XML schemas. Otherwise, I think, it would be complete absurd to have an XML schema language expressed in the form XML and not to have an XSD description of it!


For that matter, there is even an XML-based programming language called XSLT (very important in XML technologies), and, you guess, there is an XML schema for it too:

http://www.w3.org/2007/schema-for-xslt20.xsd

ColdFusion
  • 2,381
  • 13
  • 14
  • Just pointing out that this "schema for schemas" also appears in the xml schema specification itself, in appendix A: http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#normative-schemaSchema But it can't capture all the constraints of XSD (i.e. so that validity with respect to this "schema for schemas" doesn't guarantee correctness). – 13ren Jul 19 '13 at 12:35
  • The schema is broken and fails validation. Why bother having an XSD schema which fails its own check. W3C is do as they say not as they do. – ATL_DEV Jul 10 '22 at 04:18
5

The Wikipedia statement that XSD is "not 100% self describing" is correct in the sense that not every document conforming to the schema-for-schema-documents (S4SD) is capable of producing a valid schema. One reason for this is that there are constraints that cannot be expressed in XSD (for example, that the content of xpath attributes should be syntactically-valid XPath expressions); another is that XSD can only express constraints on a single document, whereas for a schema to be valid, there are consistency constraints that apply across documents.

If the S4SD took full advantage of XSD 1.1 capabilities then it would be possible to get much closer to 100% coverage of all the "XML representation" rules; I was hoping to attempt this but it never got done. There would still be a few gaps.

Your plan to write software that processes schema documents is one you should think about carefully. It's not easy to extract information from raw schema documents because there are so many different ways a schema author can express the same thing. The alternative is to work with an API that offers access to the "schema components" (such as element declarations and type definitions) that a schema processor creates from the raw input documents. Several schema processors offer such an API. Saxon-EE delivers the schema components in an XML representation called SCM, which is much easier to process than the raw XSD documents.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164