XSD Schema for XML with multiple structures

Posted by Xetius on Stack Overflow See other posts from Stack Overflow or by Xetius
Published on 2010-03-11T16:08:16Z Indexed on 2010/03/12 3:07 UTC
Read the original article Hit count: 445

Filed under:
|
|

I am attempting to write an XML Schema to cover a number of XML conditions which I may encounter. I have the same root element (serviceRequest) with different child elements. I was trying to use the xs:extension element to define multiple versions, but it is complaining about unexpected element orderInclusionCriteria etc.

Am I going about this the right way, or is there a better way to define this?

The other way I thought about this was to have a single xs:choice with all the options inside it, but this seemed somewhat inelegant.

These XSD files are for use within XMLBeans if that makes any difference.

I have Given the following 2 examples of XML:

1)

<?xml version="1.0" encoding="utf-8"?>
<serviceRequest method="GOO" debug="NO">
    <sessionId sID="ABC1234567" />
    <orderInclusionCriteria accountId="1234567" accountNum="1234567890" />
</serviceRequest>

2)

<?xml version="1.0" encoding="utf-8"?>
<serviceRequest method="GOO" debug="NO">
    <sessionId sID="ABC1234567" />
    <action aType='MakePayment'>
        <makePayment accountID='CH91015165S' amount='5.00' />
    </action>
</serviceRequest>

I thought I could use the following schema file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="serviceRequest" type="ServiceRequestType" />
    <xs:element name="session" type="SessionType" />

    <xs:attribute name="method" type="xs:string" />
    <xs:attribute name="debug" type="xs:string" />

    <xs:complexType name="SessionType">
        <xs:attribute name="sID" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:string"/>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>

    <xs:complexType name="ServiceRequestType">
        <xs:sequence>
            <xs:element ref="session" />
        </xs:sequence>
        <xs:attribute ref="method" />
        <xs:attribute ref="debug" />        
    </xs:complexType>

    <xs:complexType name="OrderTrackingServiceRequest">
        <xs:complexContent>
            <xs:extension base="ServiceRequestType">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="OrderInclusionCriteria" type="xs:string" />
                    </xs:sequence>
                </xs:complexType>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="Action">
        <xs:complexContent>
            <xs:extension base="ServiceRequestType">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="makePayment">
                            <xs:complexType>
                                <xs:attribute name="accountID" type="xs:string" />
                                <xs:attribute name="amount" type="xs:string" />
                            <xs:complexType>
                        </xs:element>
                    </xs:sequence>
                    <xs:attribute name="aType" type="xs:string" />
                </xs:complexType>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

© Stack Overflow or respective owner

Related posts about xsd

Related posts about xml-schema