0

How can we write XSD schema for below XML using XSD 1.1 because using XSD 1.0 I am facing some issue as we are using same element name with diff types(attribute name and types) which is invalid and ambiguous in XSD 1.0 and I think in XSD 1.1 there could be some way (may be alternatives/assertions)to get a work around for this issue.

<?xml version="1.0" encoding="UTF-8"?>
<SETTINGS>
    <CLEANING amount="40"/>
    <CLEANING name="abcd"/>
    <CLEANING value="0.01"/>
</SETTINGS>

Unfortunately we can not change XML format(which containes multiple elements with diff attribute types) and we need to write XSD schema for this XML with restrictions on attribute values.

As per the comments below created schema as below but still getting error in validation of above xml.

<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xs:element name="SETTINGS" type="settingsType"/>

    <xs:element name="CLEANING" type="cleaningType">
        <xs:alternative type="amountType" test="exists(@amount)"/>
        <xs:alternative type="nameType" test="exists(@name)"/>
        <xs:alternative type="valueType" test="exists(@value)"/>
    </xs:element>

    <xs:complexType name="settingsType">
        <xs:sequence>
            <xs:element ref="CLEANING" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>


    <!-- Base type -->
    <xs:complexType name="cleaningType">
    </xs:complexType>


    <!-- Type amount -->
    <xs:complexType name="amountType">
        <xs:complexContent>
            <xs:extension base="cleaningType">
                <xs:attribute name="amount" type="xs:integer" use="required"/>
                <xs:anyAttribute />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <!-- Type name -->
    <xs:complexType name="nameType">
        <xs:complexContent>
            <xs:extension base="cleaningType">
                <xs:attribute name="name" type="xs:string" use="required"/>
                <xs:anyAttribute />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <!-- Type value -->
    <xs:complexType name="valueType">
        <xs:complexContent>
            <xs:extension base="cleaningType">
                <xs:attribute name="value" type="xs:float" use="required"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

</xs:schema>

Errors Severity Location Filename Message

7:66 schema.xsd s4s-elt-must-match.1: The content of 'CLEANING' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: alternative.

4:24 sample.xml cvc-complex-type.3.2.2: Attribute 'name' is not allowed to appear in element 'CLEANING'.

Jack
  • 694
  • 8
  • 20
  • 1
    Yes you can do this with type alternatives. See the cited duplicate. – Michael Kay Dec 20 '18 at 14:32
  • @Michael i have seen that example but in that example they are using common attribute "mode" as a basis to decide/test the type to use but in my case the duplicate CLEANING elements have different attributes and nothing common to test against. – Jack Dec 20 '18 at 14:38
  • 1
    It's only an example. You can put in any conditions you like, there doesn't have to be a common attribute. One condition could be `exists(@amount)`, another could be `exists(@name)`. – Michael Kay Dec 20 '18 at 14:41
  • @MichaelKay tried as per your suggestions still getting error while validating xml against it. Anything I am doing wrong here ? – Jack Dec 26 '18 at 10:12
  • Looks as if you are using a schema processor that does not support XSD 1.1. – Michael Kay Dec 26 '18 at 23:22

0 Answers0