0

Facing issues with XML validation. My XML data look like this

<Sections>
          <Section Id="DateAndTimeSection" Enable="True" >
            <Column Id="Date" Format="YYYYMMDD" />
            <Column Id="Time" Format="HHMMSS" />
          </Section>
          <Section Id="ComponentIdSection" Enable="True" >
            <Column Id="ComponentId" Title="COUNTER" Enable="True" />
          </Section>
    </Sections>

I need to verify the XML document based on the "Id" attribute of the "Section" element.

If the Id value of the section is DateAndTimeSection then it should support Column's only with the Id's Date and Time. If any Columns other than the above are present ie. with Id value other than Date or Time it should be notified as an invalid XML.

Example: Valid XML

<Section Id="DateAndTimeSection" Enable="True" >
        <Column Id="Date" Format="YYYYMMDD" />
        <Column Id="Time" Format="HHMMSS" />
</Section>

Invalid XML

<Section Id="DateAndTimeSection" Enable="True" >
         <Column Id="Date" Format="YYYYMMDD" />
         <Column Id="Example" Format="YYYY" />
</Section>

I tried with the XML Schema and Schematron based validation.

  • Would you please give us more details about your concrete question? In general it should be possible with a combination of XSD and Schematron. Do you have problems with that or do you need a single-technology-solution? (see Michaels answer) – Nico Kutscherauer Apr 03 '18 at 11:55
  • Would you please give an example with the combination of XSD and Schematron – Alif Meerasahib Apr 04 '18 at 09:25

2 Answers2

0

You can achieve this using the "conditional type assignment" (also caled "type alternatives") feature in XSD 1.1. It can't be done with XML Schema 1.0.

Type alternatives allow the schema to say that the type of an element depends on the values of the element's attributes.

XSD 1.1 is implemented in Altova, Saxon, and Xerces, but not for example in Microsoft's XSD processor.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

XSD (restricted to the interesting parts and just a draft):

<xs:element name="Sections">
    <xs:complexType>
        <xs:sequence>
            <!-- other content?-->
            <xs:element ref="Section" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="Section">
    <xs:complexType>

        <xs:sequence>
            <!-- other content?-->
            <xs:element ref="Column" maxOccurs="unbounded"/>
        </xs:sequence>

        <xs:attribute name="Id" use="required" type="xs:string"/>
        <!--         other attribute definitions   -->
    </xs:complexType>
</xs:element>

<xs:element name="Column">
    <xs:complexType>
        <!-- other content or empty?-->
        <xs:attribute name="Id" use="required" type="xs:string"/>
        <!--         other attribute definitions   -->
    </xs:complexType>
</xs:element>

Schematron:

<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2" xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
    <sch:pattern>
        <!-- check only Columns in DateAndTimeSections-->
        <sch:rule context="Section[@Id = 'DateAndTimeSection']/Column">
            <!-- Check that ID is Date or Time -->
            <sch:assert test="@Id = 'Date' or @Id = 'Time'">Columns in DateAndTimeSections should have the ID Date or Time.</sch:assert>
        </sch:rule>
    </sch:pattern>
</sch:schema>