0

Suppose I have an element A and an element B extended from A as shown below.

<xsd:complexType name="A">
  <xsd:sequence>
    <xs:element name="desiredVariable" type="xs:string"/>
  </xsd:sequence>
<xsd:complexType>

<xsd:complexType name="B">
  <xsd:complexContent>
    <xsd:extension base="A">
      <xsd:sequence>
        <xs:element name="anotherVariable" type="xs:string"/>
      </xsd:sequence>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>

I have an usecase where desiredVariable in A can take any value and the same desiredVariable in B should be a fixed value. ie I have to apply restiction on desiredValue in B. How can I do that?

ganesshkumar
  • 1,317
  • 1
  • 16
  • 35

1 Answers1

0

In general, XML 1.0 does not allow conditional data. But you can acheive what you want through extensions. This is how to implement it in XML 1.0:

You'll need to define the base element as abstract if you want to enforce the limitations. In the XML file they will need to specify the extension they are implementing. You need to define your restrictions separately.

XSD:

<xsd:complexType name="A" abstract="true">
  <xsd:sequence>
    <xs:element name="desiredVariable" type="xs:string"/>
  </xsd:sequence>
<xsd:complexType>

<xsd:complexType name="B">
  <xsd:complexContent>
    <xsd:extension base="A">
      <xsd:restriction base="checksumType">
        <xsd:sequence>
          <xs:element name="desiredVariable" type="xs:string" fixed="FixedValue"/>
          <xs:element name="anotherVariable" type="xs:stringLimitedType"/>
        </xsd:sequence>
      </xsd:restriction>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>

<xsd:simpleType name="stringLimitedType">
    <xsd:restriction base="xs:string">
        <xsd:pattern value="([a-zA-Z0-9])*"/>
    </xsd:restriction>
</xsd:simpleType>

XML:

<A namespace:type="B">
    ...
</A>

See the following for more on abstraction and extensions: XSD schema abstract type problem

I have heard there are more options when using XML 1.1.

Community
  • 1
  • 1
VoteCoffee
  • 4,692
  • 1
  • 41
  • 44