Question

Goals:

  1. Create an XSD where the "type" attribute is required for every xs:element defined in the schema

  2. Be able to re-use the redefined http://www.w3.org/2001/XMLSchema in other schemas to force all defined xs:element(s) to require the "type" attribute

For example, I would like the following to be "not valid" in our XSD (e.g. in XMLSpy)

<xs:element name="SomeElement"/>

whereas the following would be valid

<xs:element name="SomeElement" type="abc:SomeType"/>

Here is an example of a schema where I have attempted to redefine the <xs:complexType name="element"> to require the "type" attribute.

<?xml version="1.0"?>
<!-- edited with XMLSpy v2013 (http://www.altova.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:redefine schemaLocation="http://www.w3.org/2001/XMLSchema.xsd">
        <xs:complexType name="element" abstract="true">
            <xs:complexContent>
                <xs:restriction base="xs:element">
                    <xs:attribute name="type" use="required">
                        <xs:simpleType>
                            <xs:restriction base="xs:QName"/>
                        </xs:simpleType>
                    </xs:attribute>
                </xs:restriction>
            </xs:complexContent>
        </xs:complexType>
        <xs:complexType name="topLevelElement">
            <xs:complexContent>
                <xs:restriction base="xs:topLevelElement"/>
            </xs:complexContent>
        </xs:complexType>
        <xs:complexType name="localElement">
            <xs:complexContent>
                <xs:restriction base="xs:localElement"/>
            </xs:complexContent>
        </xs:complexType>
        <xs:complexType name="narrowMaxMin">
            <xs:complexContent>
                <xs:restriction base="xs:narrowMaxMin"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:redefine>
    <xs:element name="SomeElement"/>
</xs:schema>

Now, there are some interesting aspects of this schema, and some odd behavior in XMLSpy 2013 (no service pack):

  1. In "Text" view, and attempting to save, XMLSpy indicates the schema is "not valid"

  2. In "Schema" view, and attempting to save, XMLSpy indicates the schema is valid

  3. An attempt to create a sample XML file in XMLSpy will result in an error indicating the schema is not valid

  4. The only part of the schema that should not be valid is the <xs:element name="SomeElement"> because it has not been defined with a "type" attribute.

  5. The errors that occur are related to duplicate declarations; but what is being attempted is a redefinition rather than another declaration.

Questions:

  1. Is it possible to redefine <xs:complexType name="element"> to require the "type" attribute?
  2. Is it possible to use this redefined type in other XSD(s) with a different "targetNamespace"?
Was it helpful?

Solution

Since "XSD" is the W3C XML schema language controlled by W3C, you cannot redefine anything in it. Specifically, you cannot redefine anything defined in http://www.w3.org/2001/XMLSchema namespace!

And how could you? After all, whatever you do, you always need to start from the normal <xs:schema> element, which is already defined in that namespace, and 'defined' means here that any possible content of <xs:schema> is already defined as well. There cannot be some kind of bootstrapping here! That would be a different language then. (Although a curious idea indeed -- write it down and send to W3C!)

Well, what you can do is to define your own XML schema language (e.g. 'XSDX')... but in different namespace, and, yes, you can base it on the standard XSD. Just import http://www.w3.org/2001/XMLSchema namespace and reuse any constructions defined there (that is global components). What could be use of that 'XSDX' language is a different story... (I guess, you'd have to develop some kind of new software to work with it.)

However, I guess, your actual goal is to validate your XML schemas for compliance with some aditional requirements (e.g. about that "type" attribute).

You can achive this by developing some kind of extra-validator.

Alternatively, you can replace (temporary) the http://www.w3.org/2001/XMLSchema string in your schema(s) with the URI representing your extended XML schema language. Then, any standard software would think it is just yet another XML and do with it any validations or whatever else. However, a full-blown defintion of your extended XML schema language -- in the form of some (normal) XML schema for it -- will still be needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top