views:

30

answers:

1

I have several elements defined in my XSD file that I use as references later on in the document. I do want any of these "reference" elements to constitute a valid xml file.

For example I have

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="Section">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Section" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="code"/>
            <xs:attribute name="url"/>
            <xs:attribute name="isLegacy"/>
            <xs:attribute name="name"/>
            <xs:attribute name="helpFileName"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="Sections">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Section" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

I don't want the following xml to validate (if this is the only line in the file)

<Section code="" url="" isLegacy="" name="" helpFileName="" />

Removing the "Section" node from the global prevents me from referencing it for recursion

+2  A: 

If you want that, don't declare these elements as global, instead, base your schema design on complex types and declare only the element you want as a root global. Nobody forces you to make every element global.

For example, your sample can be refactored as follows:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:complexType name="Section">
        <xs:sequence>
            <xs:element name="Section" type="Section" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="code"/>
        <xs:attribute name="url"/>
        <xs:attribute name="isLegacy"/>
        <xs:attribute name="name"/>
        <xs:attribute name="helpFileName"/>
    </xs:complexType>
    <xs:element name="Sections">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Section" type="Section" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
xcut
edited original post to provide a better example of what I'm trying to accomplish
Tom
updated my example accordingly; the answer remains the same
xcut
using xmlspy to validate that xsd, I still get the same problem of when you say ref="Section", it bitches that "'Section' must refer to an existing element". Anyway around that?
Tom
Oops; editing mistake, I removed that element reference and replaced it with a local element. n.b. you might want to read up on xml schema complex types, it's worth your time!
xcut
excellent, that worked exactly how I wanted to. Thanks very much for the prompt and numerous responses!
Tom