0

This is a continuity of My question

Assume below is my xsd file,

Updated XSD file

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/wm" xmlns="http://www.example.com/wm" elementFormDefault="qualified" version="1.0">
    <xsd:include schemaLocation="Comm.xsd" />
    <xsd:element name="testEame1">
        <xsd:annotation>
            <xsd:documentation>       test       </xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="id" type="xsd:string" minOccurs="1" />
                <xsd:element name="session" type="rawSess" minOccurs="1" />
            </xsd:sequence>
            <xsd:attribute name="pid" type="xsd:integer" use="required" />
            <xsd:attribute name="version" type="xsd:string" use="required" />
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

references another XSD which has type defined,

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" targetNamespace="http://www.example.com/wm" xmlns="http://www.example.com/wm" elementFormDefault="qualified">
    <xsd:complexType name="rawSess">
        <xsd:sequence>
            <xsd:element name="oldSessionVersion" type="Sessiontype1" />
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="sessInfo">
        <xsd:sequence>
            <xsd:element name="newSessionVersion" type="Sessiontype2" />
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Sessiontype1">
        <xsd:sequence>
            <xsd:element name="ele1" type="xsd:string" minOccurs="0" />
            <xsd:element name="ele2" type="xsd:string" />
            <xsd:element name="ele3" type="xsd:string" minOccurs="0" />
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Sessiontype2">
        <xsd:sequence>
            <xsd:element name="nele1" type="xsd:integer" minOccurs="0" />
            <xsd:element name="nele2" type="xsd:integer" />
            <xsd:element name="nele3" type="xsd:integer" minOccurs="0" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

In the above xml, for element session , I want it to have only two types like below. Either sessInfo or rawSess

<xsd:element name="session" type='sessInfo| rawSess' minOccurs="1"/>

Update Note : userDefinedtypes are complex types.

How can I configure my xsd to achieve this

2

1 Answer 1

1

If userDefinedType1 and userDefinedType2 are simple atomic types, then define a union type:

<xs:element name="session" type="one-or-two"/>

<xs:simpleType name="one-or-two">
  <xs:union memberTypes="userDefinedType1 userDefinedType2"/>
</xs:simpleType>

We now know that they are actually complex types. Define a type

<xs:complexType name="eitherType">
  <xs:choice>
    <xs:sequence>... content model of alternative 1</xs:sequence>
    <xs:sequence>... content model of alternative 2</xs:sequence>
  </xs:choice>
</xs:complexType>

and reference this type from the element declaration.

NB: this only works if there is no ambiguity, that is, if the validator can work out which branch of the choice to take based on the first element name that it sees in the instance document. If your choice were between two content models that both start with an h1 element, for example, you would have to reorganise the content models to remove the ambiguity.

3
  • my userDefinedtypes are complex types. Does it work for complex types?
    – theRoot
    Oct 3, 2018 at 5:50
  • No, if there are several possible complex types for an element you have to tell the system which one to validate against. You can use an xs:choice content model (in which case it depends on the name of the first child element found) or in XSD 1.1 you can use type alternatives (conditional type assignment) in which case it depends on the value of an attribute. You could also use the xsi:type mechanism where the instance document explicitly nominates one of the possible types (which will be defined in the schema as types derived by restriction or extension from some base type). Oct 3, 2018 at 7:33
  • It would help at this stage to give examples of the XML instance documents you want to be valid. Oct 3, 2018 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.