The requirement that you have mentioned is not possible to achieve with XSD. As an XSD developer, by reading your question what I understand is .. you want to validate the hierarchy and the data under the nodes rather than the name of the nodes. So you would like to maintain a same XSD for all the variety of incoming XML.
Well. You may not find a perfect solution for this.. but I have a workaround for you if you are interested. But for this you you need to know the possible names..
In XSD we have an option of <Choice/>
which allows to validate one among the multiple possible nodes. The validation will be focused on Hierarchy and Data, rather than Name of the node.
In the XML samples mentioned below, we have either a definition of AppleFruit or Orange fruit. Though the tag names be different but hierarchy and data types are same:
<?xml version="1.0" encoding="utf-8"?>
<root>
<AppleTree>
<AppleFruitColor>Red</AppleFruitColor>
<AppleFruitShape>Heart</AppleFruitShape>
<Tastes>Sweet</Tastes>
<Price>$1</Price>
</AppleTree>
<AppleTree>
<AppleFruitColor>Green</AppleFruitColor>
<AppleFruitShape>Heart</AppleFruitShape>
<Tastes>Sweet and Sour</Tastes>
<Price>$0.5</Price>
</AppleTree>
</root>
second XML:
<root>
<OrangeTree>
<OrangeFruitColor>Orange</OrangeFruitColor>
<OrangeFruitShape>Round</OrangeFruitShape>
<Tastes>Sweet and Sour</Tastes>
<Price>$0.5</Price>
</OrangeTree>
</root>
The relevant XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="root"/>
<xs:complexType name="root">
<xs:sequence>
<xs:choice>
<xs:element maxOccurs="unbounded" name="AppleTree" type="Tree"/>
<xs:element maxOccurs="unbounded" name="OrangeTree" type="Tree"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Tree">
<xs:sequence>
<xs:choice>
<xs:element name="AppleFruitColor" type="FruitCOLOR" />
<xs:element name="OrangeFruitColor" type="FruitCOLOR" />
</xs:choice>
<xs:choice>
<xs:element name="AppleFruitShape" type="xs:string" />
<xs:element name="OrangeFruitShape" type="xs:string" />
</xs:choice>
<xs:element name="Tastes" type="xs:string" />
<xs:element name="Price" type="xs:string" />
</xs:sequence>
</xs:complexType>
<!--Custom Data Types-->
<xs:simpleType name="FruitCOLOR">
<xs:restriction base="xs:string">
<xs:enumeration value="Red"/>
<xs:enumeration value="Green"/>
<xs:enumeration value="Orange"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Note: You can define separate ComplexTypes/SimpleTypes for each element (unlike eg, I am reusing complexType "Tree")
Explanation:
In the above XSD, root element might have AppleTree or OrangeTree or both.. The next level of ComplexType Tree validates nodes under AppleTree/OrangeTree,
For example: AppleFruitColor or OrangeFruitColor can have data none other than "Red or Green Or Orange" .. The XSD is flexible so that it can accept OrangeFruitColor under AppleTree that means, we are not bothered which node is coming under AppleTree but we are bothered about what fruit color it is having !!
In the above example I could have defined separate Fruit colors for both Apple and Orange:
<xs:complexType name="Tree">
<xs:sequence>
<xs:choice>
<xs:element name="AppleFruitColor" type="AppleCOLOR" />
<xs:element name="OrangeFruitColor" type="OrangeCOLOR" />
</xs:choice>
........
......
<xs:simpleType name="AppleCOLOR">
<xs:restriction base="xs:string">
<xs:enumeration value="Red"/>
<xs:enumeration value="Green"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="OrangeCOLOR">
<xs:restriction base="xs:string">
<xs:enumeration value="Orange"/>
</xs:restriction>
</xs:simpleType>
With this code, Schema accepts, <OrangeFruitColor>
to have only Orange color where as <AppleFriutColor>
can be either Red or Green.
The validation is focused on Hierarchy and Data, rather than Name of the node.