capaul1977
capaul1977

Reputation: 1

XSD Required Elements with specific child elements (Multiple Definitions with different types)

All, I have an XML doc which I don't control for which I need to create an xsd to validate. The XML doc has multiple transaction types, some of which are required a specific number of times, and some aren't. the parent element is simply <transaction>, the child element can be either a <ControlTransaction> or a <RetailTransaction>. The issue is that I need to require a <transaction> to exists with a <ControlTransaction> with a <ReasonCode> element having a value of "Register Open" and another with a value of "Register Close" as follows:

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

<Transaction>
    <SequenceNumber>1</SequenceNumber>
    <ControlTransaction>
        <ReasonCode>Register Open</ReasonCode>
    </ControlTransaction>
</Transaction>

<Transaction>
    <SequenceNumber>2</SequenceNumber>
    <RetailTransaction>
        ...stuff..
        <Total>9.99</Total>
    </RetailTransaction>
</Transaction>

<Transaction>
    <SequenceNumber>3</SequenceNumber>
    <ControlTransaction>
        <ReasonCode>Register Close</ReasonCode>
    </ControlTransaction>
</Transaction>

</RegisterDay>

My best attempt is to use types in my schema, but get "Elements with the same name and same scope must have the same type". I don't know how to get around this.

<?xml version="1.0"?>
<xs:schema 
        xmlns:cp="urn:register" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        attributeFormDefault="unqualified" 
        elementFormDefault="qualified">

    <xs:element name="RegisterDay">     
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="1" maxOccurs="1" name="Transaction" type="TransactionRegisterOpen_type"/>
                <xs:element minOccurs="1" maxOccurs="unbounded" name="Transaction" type="RetailTransaction_type"/>
                <xs:element minOccurs="1" maxOccurs="1" name="Transaction" type="TransactionRegisterClose_type"/>

            </xs:sequence>
        </xs:complexType>
    </xs:element>       

    <xs:simpleType name="RegisterOpen_type">
        <xs:restriction base="xs:string">
            <xs:pattern value="Register Open"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="RegisterClose_type">
        <xs:restriction base="xs:string">
            <xs:pattern value="Register Close"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="TransactionRegisterOpen_type">
        <xs:sequence>
            <xs:element name="SequenceNumber" type="xs:unsignedShort"/>
            <xs:element name="ControlTransaction">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="1" name="ReasonCode" type="RegisterOpen_type"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>   

    <xs:complexType name="TransactionRegisterClose_type">
        <xs:sequence>
            <xs:element name="SequenceNumber" type="xs:unsignedShort"/>
            <xs:element name="ControlTransaction">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="1" name="ReasonCode" type="RegisterClose_type"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>   

    <xs:complexType name="RetailTransaction_type">
        <xs:sequence>
            <xs:element name="SequenceNumber" type="xs:unsignedShort"/>
            <xs:element name="ControlTransaction">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="1" name="Total" type="xs:decimal"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>           

</xs:schema>

Has anyone run into this and/or have any suggestions? I'm pretty much stumped.

Upvotes: 0

Views: 438

Answers (1)

Istao
Istao

Reputation: 7585

Perhaps with enumeration ?

<?xml version="1.0"?>
<xs:schema 
    xmlns:cp="urn:register" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    attributeFormDefault="unqualified" 
    elementFormDefault="qualified"
    targetNamespace="urn:register">

<xs:element name="RegisterDay">     
    <xs:complexType>
        <xs:sequence>
            <xs:element 
              minOccurs="1" 
              maxOccurs="unbounded" 
              name="Transaction" 
              type="cp:TypeTransaction"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>       

<xs:complexType name="TypeTransaction">
    <xs:sequence>
        <xs:element name="SequenceNumber" type="xs:unsignedShort"/>
        <xs:choice>
          <xs:element name="RetailTransaction"/>
          <xs:element name="ControlTransaction">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ReasonCode">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:enumeration value="Register Open"/> 
                      <xs:enumeration value="Register Close"/> 
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>  
    </xs:sequence>
</xs:complexType>           

</xs:schema>

Upvotes: 0

Related Questions