Home > Enterprise >  Is it possible to create XSD unique constraint based combination of element and node with multiple e
Is it possible to create XSD unique constraint based combination of element and node with multiple e

Time:04-27

Having the following xml code

<repository>
    <software id="1">
        <versions>
            <version>version1</version>
            <version>version2</version>
        <versions>
        <name>name1</name>
    </software>
    <software id="2">
        <versions>
            <version>version3</version>
        <versions>
        <name>name1</name>
    </software>
    <software id="3">
        <versions>
            <version>version3</version>
        <versions>
        <name>name1</name>
    </software>
</repository>

is it possible to create xsd constraint based on both names and versions nodes? Software 1 and 2 are unique while software 2 and 3 are not unique.

CodePudding user response:

No, the XSD xs:unique mechanism requires a one-to-one correspondence between the fields used in the constraint.

You can do it, of course, using XSD 1.1 assertions - but since you've only given an example and not a specification, I'm not sure exactly what the assertion would be.

CodePudding user response:

Using an assertion in XSD 1.1 might work e.g.

   <xs:element name="repository">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" ref="software"/>
            </xs:sequence>
            <xs:assert id="unique-versions-and-name"
              test="every $software in software
                   satisfies 
                     not(some $other-software in software[not(. is $software)] satisfies deep-equal($software/*, $other-software/*))"/>
        </xs:complexType>
    </xs:element>

I have used the descriptive id unique-versions-and-name although technically later on, in XPath, I check simply for the same child elements with e.g. deep-equal($software/*, $other-software/*) so perhaps you rather want deep-equal($software/(versions, name), $other-software/(versions, name)).

  • Related