1
votes

I have two request types which internally use the same compleType element. The distinguishing factor between the two requests is an element attribute value (apart from the request wrapper). Need to add this condition in XSD to use it as part of schema validation.

Here is the sample elements declaration

<xsd:element name="searchRequest" type="searchRequest_Type"/>
<xsd:complexType name="searchRequest_Type">
    <xsd:sequence>
         <xsd:element ref="bip:BIPOrder" minOccurs="1">                 
         </xsd:element>
    </xsd:sequence>
</xsd:complexType>


<xsd:element name="advanceSearchRequest" type="advanceSearchRequest_Type"/>
<xsd:complexType name="advanceSearchRequest_Type">
    <xsd:sequence>
         <xsd:element ref="bip:BIPOrder" minOccurs="1">                 
         </xsd:element>
    </xsd:sequence>
</xsd:complexType>

BIPOrder is a complexType with many other nested ComplexType elements inside it. One of the Element attribute value in one of the complexType is the distinguishing factor between the above two requests.

Like

<myap:searchRequest>
    <bip:BIPOrder>
        <....>
            <....>
            <BIPSearchType id="459">Normal Search</BIPSearchType>
            <....>
    </bip:BIPOrder>
</myap:searchRequest>

<myap:advanceSearchRequest>
    <bip:BIPOrder>
        <....>
            <....>
            <BIPSearchType id="479">Advanced Search</BIPSearchType>
            <...>           
    </bip:BIPOrder>
</myap:advanceSearchRequest>

I am looking to add the restriction on BIPSearchType "id" attribute value in my XSD so that when the request XML is validated it should satisfy this condition also.

Currently I am able to validate the whole request via XSD except this condition. I am explicitly validating this condition after the schema validation to check correct "BIPSearchType" "id" is sent along with the request.

Please advise on how I can acheive this in my XSD.

Thanks in advance for your kind help.

2
You say the two uses of the type are distinguished only by the id attribute, but your description says otherwise: they are also distinguished by the element names of the parent elements of bip:BIPOrder. (Short version: you have violated the principle of DRY; the universe will punish you.) - C. M. Sperberg-McQueen
I was mentioning only about the type. Though both the requests follow the same schema with same root element(excluding the request wrapper) there are many other elements that they differ. But my intention is to schema validate them based on the type id, which I am currently doing explicitly. - user1760178
have you solved this issue? if yes, how did you solve it? - Xstian
Not yet. Still looking for a solution. - user1760178

2 Answers

3
votes

You won'tlike this, but nevertheless:

This is XML Schema for a type according to your element <BIPSearchType> with id restricted to 459.

<xs:complexType name="BIPSearchTypeType459">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="id">
        <xs:simpleType>
          <xs:restriction base='xs:int'>
            <xs:minInclusive value='459'/>
            <xs:maxInclusive value='459'/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

Another type can be defined with the facets for 479.

Clearly, constructing the upper levels twice is absolutely not attractive, but facets (such as min/maxInclusive) are part of type definitions, and as soon as there is two different types, otherwise identical types differing in a single element from those two are different, too.

I can't think of anything simpler than what you probably use now.

2
votes

You should add an XSD Element Dependency Restrictions. If you're doing only XSD 1.0, then it is not possible. If you're willing to combine an XSD 1.0 processor with a Schematron engine, then you could do it.

Anyway, if you have access to an XSD 1.1 processor, then you could resolve it with an <xsd:assert/> or you could use type alternatives. Take a look here for additional information.

I hope it is what you're looking for :)