SimpleJack
SimpleJack

Reputation: 13

Is it a legal way to have the same element with different types

So i have been making a schema files for a single large XML file, that include other schema files , and in this XML file there is an element that is used several time but with different types and have their own schema file. The internet told me that you cant have elements with the same name with different types, so i tried to work around it, and here is what i tried.

Like this.

<ele1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="C:\Users\Desktop\Configuration.xsd">
    <ele2>
        <Test Name="Timetable" important="no" xsi:type="TimetableType">
            <Class>className.something</Class>
        </Test>
        <Test Name="Database" important="yes" xsi:type="DatabaseType">
            <Class>className.something</Class>
            <Database>jk</Database>
            <PortNr>1<PortNr>
        </Test>
    </ele2>
</ele1>

and here is it's schema file, this is where the real question lies

<xs:schema elementFormDefault="qualified"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:include schemaLocation="file:///C:\Users\Desktop\Timetable.xsd" />
<xs:include schemaLocation="file:///C:\Users\Desktop\Database.xsd" />

<xs:element name="ele1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="ele2">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Test" xsi:type="TimetableType" />
                        <xs:element name="Test" xsi:type="DatabaseType" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Is it a legal way to make a schema file, using the xsi:type. For validation program i am using Liquid XML Studio 2014, and it is validating the Configuration file.

Upvotes: 0

Views: 58

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

The xsi:type attribute is never used in a schema, only in an instance document.

There is a constraint in the XSD specification that says two sibling elements with the same name must have the same type. It's called the "element declarations consistent" constraint. Don't try to get around it: you can't.

Upvotes: 1

Related Questions