1

I need to write a XSD which will validate the attribute as well as the value of the element. So far I am able to validate the attributes but not the value. Thanks for your help in advance.

My XML:

<params>
    <param dataType="java.lang.String">12A</param>
    <param dataType="java.lang.Integer">6455</param>
    <param dataType="oracle.jbo.domain.Date">2014-10-01</param>
    <param dataType="oracle.jbo.domain.Date">2018-10-01</param>
    <param dataType="java.lang.String">H1</param>
    <param dataType="java.lang.String">4235AS</param>
    <param dataType="java.lang.String">5aZ</param>
    <param dataType="java.lang.String">q2w</param>
    <param dataType="java.lang.Integer">10</param>
    <param dataType="java.lang.Integer">3</param>
</params>

My XSD

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="params">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="10" 
                    name="param" type="paramType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="paramType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="dataType" type="validAttributeType" use="optional" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:simpleType name="validAttributeType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="java.lang.String" />
      <xs:enumeration value="java.lang.Integer" />
      <xs:enumeration value="oracle.jbo.domain.Date" />

    </xs:restriction>
  </xs:simpleType>
</xs:schema>

So my need is, if the dataType says Integer it should only accept numbers as value or if it says date, then the value should be date not anything else.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Sammy
  • 121
  • 4
  • 12

1 Answers1

0

XSD 1.0

Type checking based on an attribute value cannot be done in XSD 1.0.

XSD 1.1

You can use Condition Type Assignment:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">

  <xs:element name="params">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="10" name="param">
          <xs:alternative test="@dataType='java.lang.String'" type="xs:string"/> 
          <xs:alternative test="@dataType='java.lang.Integer'" type="xs:integer"/> 
          <xs:alternative test="@dataType='oracle.jbo.domain.Date'" type="xs:date"/> 
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

While this is the proper structure for representing attribute-based types, you should take care to note that XSD types may vary from Java types in some cases, so check to make sure that any differences are tolerable given your requirements.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Tried this one, but getting the below error: The Content Of 'param' Must Match (annotation?, (simpleType | ComplexType)?, (unique | Key | Keyref)*)). A Problem Was Found Starting At: Alternative. – Sammy Oct 29 '15 at 17:24
  • Your XSD processor must support **XSD 1.1** to be able to use Conditional Type Assignment. The error indicates that it does not understand `xs:alternative`, which a XSD 1.0 processor would not. – kjhughes Oct 29 '15 at 17:29
  • A workaround if you have to use XSD 1.0 might be to use an xsi:type attribute in place of dataType. – Michael Kay Oct 29 '15 at 19:45
  • Hi Michael, So attribute xsi:type should be used in the request not in the XSD. If this the case then I can not do that as the request structure should not be changed. Any other way to achieve this in XSD 1.0? – Sammy Nov 02 '15 at 11:03