質問

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!

役に立ちましたか?

解決

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.

他のヒント

Your XSD seems fine to me. I've tried it in QTAssistant (which is ultimately based on the Xerces version of XSD 1.1) and it works just fine.

With this sample XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
    <MyType type="Tagged"/> 
</MyRoot>

I get:

Error while loading [], line 4 position 25
cvc-complex-type.4: Attribute 'tag' must appear on element 'MyType'.
Document1.xml is XSD 1.1 invalid.

With this XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
    <MyType type="Named"/>  
</MyRoot>

I get:

Error while loading [], line 4 position 24
cvc-complex-type.4: Attribute 'path' must appear on element 'MyType'.
Document1.xml is XSD 1.1 invalid.

Fixing the above as suggested will yield a valid XML result. Your syntax is correct, so I can only blame your XSD processor.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top