0

I have an xml file with the associated xsd below:

The element "name" of complexType "UpgraderConfig" needs to be unique. I have added "unique" tag to "UpgraderConfigContainer" definition, yet cannot seem to get the desired behaviour.

Can anyone please advise as to what I am doing wrong?

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.hp.com/maas/1.0.0/UpgraderConfig"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:config="http://www.hp.com/maas/1.0.0/UpgraderConfig"
           elementFormDefault="qualified"
           version="1.0.0">

    <xs:simpleType name="stringWithoutWhiteSpace">
        <xs:annotation>
            <xs:documentation>One or more occurrences of any character which is not whitespace (i.e. not space, tab, newline, return)</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1" />
            <xs:pattern value="\S+" />
        </xs:restriction>
    </xs:simpleType>


    <xs:complexType name="UpgraderConfigContainer">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Upgrader" type="config:UpgraderConfig" >
                <xs:unique name="uniqueUgraderName">
                    <xs:selector xpath="Upgrader"/>
                    <xs:field xpath="name"/>
                </xs:unique>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="UpgraderConfig">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="className" type="config:stringWithoutWhiteSpace"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="UpgraderConfigs" type="config:UpgraderConfigContainer" />

</xs:schema>

1 Answer 1

1

You have defined the unique constrain too low. An element of itself can not be unique. Only the container containing it can have the unique constrain. You have labelled UpgraderConfigContainer a bit miss-leading, because it is not the container. Instead it is the element, which is unbound, which means that you have zero or more UpgraderConfigContainers. The element containing UpgraderConfigContainer must have the unique constrain. You have to add it to UpgraderConfigs.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:config="http://www.hp.com/maas/1.0.0/UpgraderConfig" targetNamespace="http://www.hp.com/maas/1.0.0/UpgraderConfig" elementFormDefault="qualified" version="1.0.0">
    <xs:simpleType name="stringWithoutWhiteSpace">
        <xs:annotation>
            <xs:documentation>One or more occurrences of any character which is not whitespace (i.e. not space, tab, newline, return)</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:pattern value="\S+"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="UpgraderConfigContainer">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Upgrader" type="config:UpgraderConfig"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="UpgraderConfig">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="className" type="config:stringWithoutWhiteSpace"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="UpgraderConfigs" type="config:UpgraderConfigContainer">
        <xs:unique name="uniqueUpgraderName">
            <xs:selector xpath="config:Upgrader"/>
            <xs:field xpath="config:name"/>
        </xs:unique>
    </xs:element>
</xs:schema>
1
  • I did not know the containing element only can have the unique constraint on it. Thank you, it indeed works. Commented Aug 22, 2016 at 14:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.