0

I want to use the below XSD to validate a particular XML. And at the same time the XSD should not validate few element from the below XML but the other elements should be validated.

For instance the sample XML is:

<args src="body">
    <arg name="echo1">£*138</arg>
    <arg name="echo2">a-a$138</arg>
    <arg name="echo3">b-b$136</arg>
    <arg name="echo4">£*136</arg>
</args>

And the XSD I am using is:

<xs:schema attributeFormDefault="unqualified"
  elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="args">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="arg" maxOccurs="9" minOccurs="1">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="argFilter">
                <xs:attribute name="name" use="required" type="codeEnumeration" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="src" use="required">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="url"/>
            <xs:enumeration value="body"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="codeEnumeration">
    <xs:restriction base="xs:string">
      <xs:enumeration value="echo1"/>
      <xs:enumeration value="echo2"/>
      <xs:enumeration value="echo3"/>
      <xs:enumeration value="echo4"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="argFilter">
    <xs:restriction base="xs:string">
      <xs:minLength value="0"/>
      <xs:maxLength value="256"/>
      <xs:pattern value="[a-zA-Z0-9 ,:?.\\\-_=%+@&amp;!@#$%^*()\[\]+={}|\/:;,?`~'&quot;&lt;&gt;]*"/>
     </xs:restriction>
  </xs:simpleType>
</xs:schema>

From the above XML, I do not want echo1 and echo4 to be validated, but all other element should be validated. So, basically it should allow any value in echo1 and echo4 but restrict the value of echo2 and echo3 to match the pattern value. How can I achieve this by modifying the above XSD?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Lenka
  • 75
  • 11

1 Answers1

1

You'll need to change your XML design or validate apart from XSD if you're limited to XSD 1.0. If you can use XSD 1.1, then Conditional Type Assignment will allow you to express relaxed validation constraints based upon an attribute value in XSD without changing your XML design.

See also:

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks for your reply. XML can not be changed. So obviously next option is to use XSD1.1. Could you please help how can I start? – Lenka Aug 14 '18 at 11:57
  • (1) Make certain that you have access to a XSD 1.1 processor. (2) Use the CTA link in my answer for an example of how to use CTA in XSD 1.1. (3) Post a new question on any particular difficulty you encounter. – kjhughes Aug 14 '18 at 12:10