views:

41

answers:

2

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"&gt;

    <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>
+1  A: 

Would the <xs:choice> element help: http://www.w3schools.com/Schema/el_choice.asp

Let me know if it does and if you want a full example.

Scobal
A: 

try to use minOccurs="0" for the elements you don't want to be present. I guess in this way you can solve your problem with versions. Also xs:choice can work in your case

Nikolai