Search code examples
xmlxsdxml-parsing

How to use sequence and non sequence childs in xml schema


1. I have a well-formed xml as follows

<graph>
    <id>g0</id>
    <name>test</name>
    <nodes>
        <node>
            <id>a</id>
            <name>xxxx</name>
        </node>
        <node>
            <id>e</id>
            <name>xxxx</name>
        </node>
    </nodes>
    <edges>
        <node>
            <id>e1</id>
            <from>a</from>
            <to>e</to>
        </node>
    </edges>
</graph>

2. My logic, and XSD attempt to implement it

I am trying to define a schema for my xml with the role that all elements should come in order after each other but id and name can be in any order as far as they are there here is my sechema

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="graph" type="AType"/>
<xsd:complexType name="AType">
    <xsd:sequence>
        <xsd:element name="id" type="xsd:string" />
        <xsd:element name="name" type="xsd:string" />
        <xsd:element name="nodes"  minOccurs="0">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="node" maxOccurs="unbounded" minOccurs="0">
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element type="xsd:string" name="id"/>
                                <xsd:element type="xsd:string" name="name"/>
                                <xsd:any minOccurs="0"/>
                            </xsd:sequence>
                        </xsd:complexType>
                    </xsd:element>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>
</xsd:schema>

so as you can see here my

    <id>g0</id>
<name>test</name>

is also part of the sequence and they are forced to follow the order but I do not want any order on these two as far as they are there any suggestion?

Note: please note I know all will mean order does not matter but my issue is how I can use both all and seuqence together complex type?

3. XSD processor and its conformance with the XSD standards: 1.0, or 1.1.

I am using XSD processor ... and its conformance with the XSD standards ...


Solution

  • You can't mix xs:all and xs:sequence in the same content model, but you can define a sequence that allows either

    id, name, nodes, edges
    

    or

    name, id, nodes, edges
    

    by making the content model

    sequence
       choice
          sequence
             id
             name
          sequence
             name
             id
       nodes
       edges