1
votes

Below is the xml , of which i need to create an xml schema(xsd file) validating the field name i.e. in the tableData node(in below xml), the first column name should be "field" with the key as "name" and its value should be "Data1".I also want to validate that the value of this column should be of type xs:integer(as value is 123). Similarly need for all the columns of the table.I am new to xml validation so facing issues.

XML:

<table name="Employee">
    <tableData>
      <field name="Data1">123</field>
      <field name="Data2">234</field>
      <field name="Data3">Y</field>
      <field name="Data4">321</field>
    </tableData>
</table>

I tried to create the schema but failed to validate the type of 123 (i.e. xs:integer) and other column types.Please if someone can provide a hint on this.

Schema which I tried:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
          xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tableData" >
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="4" name="field" type="fieldType" minOccurs="4"/>
      </xs:sequence>
    </xs:complexType>
    </xs:element>

  <xs:complexType name="fieldType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="name" type="validAttributeType" use="required" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:simpleType name="validAttributeType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Data1"/>
      <xs:enumeration value="Data2"/>
      <xs:enumeration value="Data3"/>
      <xs:enumeration value="Data4" />
    </xs:restriction>
  </xs:simpleType>

  </schema>

Also, for table name= "Employee" in xml( First line in xml) , I tried to create a schema and add it before the schema I mentioned above but that gives error: The element 'table' cannot contain child element 'tableData' because the parent element's content model is text only. Schema which I was adding above the schema mentioned above is:

   <xs:element name="table" type="mTable"/>
      <xs:complexType name="mTable" >
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" fixed="Employee"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
1
Can you show us the schema you have so far? That way we can see what you would need to change to make it work. - ophychius
Have added the schema which I tried so far. - user10573422
According to your sample xml a field can both contain numbers and text values . (123/234/Y/321). What exactly do you want to validate? - ophychius
I want to validate each fields type (i.e. for Data1 value is 123 so, it should be xs:integer ) .Likewise for all fields (123/234/Y/321). - user10573422

1 Answers

0
votes

It's unfortunate that you have chosen to represent the data as <field name="data1">123</field> rather than the simpler <data1>123</data1>, because this makes it very difficult to write an XSD schema for the data. In XSD, validation rules are driven by the name of the element.

There are workarounds:

  • with XSD 1.1, you could use "conditional type assignment" to assign a type to the element that is conditional on the values of its attributes. But many XSD processors do not support XSD 1.1

  • you could transform the data into the simpler form before validating it. But then the schema diagnostics will relate to the transformed document, not to the original.