Reputation: 345
Hey Guys,
this is the situation:
I have an XML-file containing many (>50) configuration-properties for a program.
This XML-content looks somehow like that:
<GlobalConfig>
<ConfigurationSettings>
<Property Name="UseColors" Value="True" Visible="False"/>
<Property Name="TitleMenu" Value="Configurator" Visible="True"/>
<Property Name="InformationText" Value="For information please read readme.txt" Visible="True"/>
[many more...]
</ConfigurationSettings>
</GlobalConfig>
What I wanted to do now, is creating an xsd-file for validating that stuff.
For each property the corresponding value-attribute has a different contenttype (with certain restrictions like enums or regEx), so the validation-rules for the content of value need to be adepted for each case. The case is determined by the "Name"-Attribute
I am new to the xsd-topic, so I tried something to start with:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="GlobalConfig">
<xs:complexType>
<xs:all>
<xs:element name="ConfigurationSettings">
<xs:complexType>
<xs:all>
<xs:element name="Property" minOccurs="0" maxOccurs="unbound">
<xs:complexType>
<xs:attribute name="Name" fixed="UseColors" type="xs:string"/>
<xs:attribute name="Value" default="True" type="xs:bool"/>
<xs:attribute name="Visible" default="False" type="xs:bool"/>
</xs:complexType>
</xs:element>
<xs:element name="Property" minOccurs="0" maxOccurs="unbound">
<xs:complexType>
<xs:attribute name="Name" fixed="TitleMenu" type="xs:string"/>
<xs:attribute name="Value" default="Title" type="xs:string"/>
<xs:attribute name="Visible" default="True" type="xs:bool"/>
</xs:complexType>
</xs:element>
<xs:element name="Property" minOccurs="0" maxOccurs="unbound">
<xs:complexType>
<xs:attribute name="Name" fixed="InformationText" type="xs:string"/>
<xs:attribute name="Value" default="See reedme.txt" type="xs:string"/>
<xs:attribute name="Visible" default="True" type="xs:bool"/>
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute type="xs:string" name="Force"/>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
This doesnt work, and I think I understand why it won't work: The problem is, that I have many elements with the same name ("Property").
I have the dim feeling, that it actually won't work at all this way and that the structure of the configuration-xml needs to be changed, so that there are elements with different names for each property.
The reason I think that, is the second answer in the following post. It seems to fullfill the needs of the questioner, but he only had two elements with the same name and he also didn't want to check their attributes: XML Schema for sequence of elements with same name but different attribute values?
Do you agree, that it won't work with the given configuration-xml structure? Or is it still possible?
Thanks a lot!
Upvotes: 2
Views: 1225
Reputation: 163595
XSD has a constraint called "Element declarations consistent" which in effect says that if two sibling elements have the same name, then they must have the same type. So it is not possible for different Property elements that are siblings of each other to have different validation rules.
In XSD 1.1 there is a solution using type alternatives. This allows you to make the type of an element conditional on the values of one or more of its attributes (in this case the Name attribute).
Is there any particular reason that you can't design the configuration file so that instead of
<Property Name="UseColors" Value="True" Visible="False"/>
you use
<UseColors Value="True" Visible="False"/>
The main reason for using the former ("generic") design is so that the schema doesn't have to know about all the possible property names. But you actually want to define the property names and valid values in your schema, so the second design is much more appropriate.
Upvotes: 2