2



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!

Community
  • 1
  • 1
and0r
  • 305
  • 1
  • 4
  • 13
  • possible duplicate of [How to use alternatives in XML Schema 1.1](http://stackoverflow.com/questions/14593284/how-to-use-alternatives-in-xml-schema-1-1) – lexicore Nov 19 '14 at 11:07
  • You can solve it with `xs:alternative` on `@name` in XSD 1.1. No standard solution in XSD 1.0. See the link I posted as duplicate. – lexicore Nov 19 '14 at 11:08

1 Answers1

2

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks for your answer. It seems to me, that redesigning of the configuration-file is the best apporach, yes. Just out of curiosity: Is using XSD 1.1 somehow bad practice? It looks like VisualStudio doesn't support it (yet?). – and0r Nov 19 '14 at 14:14
  • 1
    XSD 1.1 is extremely useful but you need to be aware that there are only three processors currently available: Saxon, Xerces, and Altova. So you need to consider whether the benefits justify the limitations on your technology choice. You should also be aware that Microsoft has pretty well stopped implementing new W3C XML standards about a decade ago, and this increasingly means that you need to be prepared to look at third party tools. – Michael Kay Nov 19 '14 at 15:41