I have gone through many site and then created Xml Schema, where the other attributes are depends on the value of one the attributes.
The below xml file:
<?xml version="1.0"?>
<Main>
<Matter kind="fruits" calories="10" name="apple" quantity="10" color="red"/>
<Matter kind="car" brand="ford" name="ikon" quantity="1" color="red"/>
<Matter kind="country" continent="Asia" name="japan"/>
</Main>
The kind attribute can have fruits, car and country. And based on the attribute other parameter are needed, like for country value, the continent attribute required.
And the below is the XSD file,
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Main">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="Matter"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Matter">
<xs:complexType>
<xs:attribute name="name" use="required" type="xs:NCName"/>
<xs:alt cond="@kind='fruits'">
<xs:attribute name="kind" use="required" type="xs:NCName" fixed="fruits"/>
<xs:attribute name="color" type="xs:NCName"/>
<xs:attribute name="quantity" type="xs:integer"/>
</xs:alt>
<xs:alt cond="@kind='car'">
<xs:attribute name="kind" use="required" type="xs:NCName" fixed="car"/>
<xs:attribute name="brand" type="xs:NCName"/>
<xs:attribute name="quantity" type="xs:integer"/>
<xs:attribute name="color" type="xs:NCName"/>
</xs:alt>
<xs:alt cond="@kind='country'">
<xs:attribute name="kind" use="required" type="xs:NCName" fixed="country"/>
<xs:attribute name="continent" type="xs:NCName" use="required"/>
</xs:alt>
</xs:complexType>
</xs:element>
</xs:schema>
When I am validating the XML file with the above XSD, I am getting the error that “Element alt is invalid, misplaced, or occurs too often.
Please help me to validate the above file, with proper XSD.
I don’t know where you got the idea of xsl:alt from. Perhaps some proprietary extension of XML Schema?
The XSD 1.1 standard has a facility called conditional type assignment that is a bit like this, with an element called xs:alternative. XSD 1.1 is supported in recent versions of Xerces and Saxon.