Is there any way to restrict root xml element name should be same as in XSD file?

100 views Asked by At

I have an XML file Which has tags like this..

<DataSt> --ROOT TAG
  <Track>
....
  </Track>
</DataSt>

AND I have XSD file Which start like this..

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="DataSt">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="Track">
          <xs:complexType>
            <xs:sequence>
              .................

I want to define some rule in XSD file the root tag name should always same as "DataSt"

for example if

<DataSt1> --ROOT TAG(wrong)
  <Track>
....
  </Track>
</DataSt1>

It should not get validated successfully and it should help to throw error.

I'm using SSIS to transfer data from XML with help of XSD file. if root tag is not same as defined in XSD file then SSIS just skipping the xml file after reading it but not throwing error.

I don't know much about XML and XSD code. Please help to solve this. Thank you!!!

1

There are 1 answers

0
Michael Kay On

The designers of XSD chose not to do this (there was a lot of debate). The rationale, as I understand it, is that a schema defines the validity of elements, not of documents (but the xs:ID/IDREF mechanism scuppers that principle).

Some schema validation APIs allow you to constrain what the top-level element should be. For example, if you do validation using Saxon from the command line, there is an option -top (see https://www.saxonica.com/documentation10/index.html#!schema-processing/commandline)