Search code examples
xsdxsd-validationxsd-1.1

XSD 1.1 alternative usage issue


I need to make specific XSD validation for a single type of node based on an attribute value: XSD 1.1 and xsd:alternative should be my friends.

BUT with the following (most simple) example:

<xsd:complexType name="BaseType">
    <xsd:attribute name="type" 
                   type="xsd:string" 
                   use="required" />
</xsd:complexType>  

<xsd:complexType name="NamedType">
    <xsd:complexContent>
        <xsd:extension base="BaseType">
            <xsd:attribute name="path" 
                           type="xsd:string" 
                           use="required" />
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>  

<xsd:complexType name="TaggedType">
    <xsd:complexContent>
        <xsd:extension base="BaseType">
            <xsd:attribute name="tag" 
                           type="xsd:string" 
                           use="required" />
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>  

<xsd:element name="MyRoot">
    <xsd:complexType>
          <xsd:choice minOccurs="1">
            <xsd:element name="MyType" 
                         type="BaseType">
              <xsd:alternative test="@type='Named'" 
                               type="NamedType"/>
              <xsd:alternative test="@type='Tagged'" 
                               type="TaggedType"/>
            </xsd:element>
          </xsd:choice>
    </xsd:complexType>
</xsd:element>

When I am loading the XSD (using QXmlSchema class from Qt 4.7.4 but I think this is an XSD issue rather than a Qt one) I get the following error:

Error XSDError in Unknown location, at line 93, column 74: test attribute of alternative element contains invalid content: {@type='Named'}.

I have tried also "@type eq 'Named'" in the alternative test condition and a ton of other sensible and less sensible variations ... none passed :/

Any help will be much appreciated! Thanks!


Solution

  • Like Petru Gardea, I believe your XSD schema is fine (and more to the point, so does Saxon).

    The problem is that your XSD processor doesn't support XSD 1.1; the QXmlSchema Class Reference says "This class is used to represent schemas that conform to the XML Schema 1.0 specification." The error message could probably be a bit clearer (by complaining about @type and not xsd:alternative it really gives the wrong idea), but that's often true of error messages, which after all typically report situations the software is not prepared to handle.