How does one restrict xml with an XML Schema?

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-06-04T23:23:13Z Indexed on 2010/06/07 14:02 UTC
Read the original article Hit count: 241

Filed under:
|

Hello,

I want to restrict xml with a schema to a specific set. I read this tutorial

http://www.w3schools.com/schema/schema_facets.asp

This seems to be what I want. So, I'm using Qt to validate this xml

<car>BMW</car>

Here is the pertinent source code.

QXmlSchema schema;

schema.load( QUrl("file:///workspace/QtExamples/ValidateXSD/car.xsd") );
if ( schema.isValid() ) {
    QXmlSchemaValidator validator( schema );

    if ( validator.validate( QUrl("file:///workspace/QtExamples/ValidateXSD/car.xml") ) ) {
        qDebug() << "instance is valid";
    } else {
        qDebug() << "instance is invalid";
    }
} else {
    qDebug() << "schema is invalid";
}

I expected the xml to match the schema definition. Unexpectedly, QxmlSchemaValidator complains.

Error XSDError in file:///workspace/QtExamples/ValidateXSD/car.xml, at line 1, column 5: Content of element car does not match its type definition: String content is not listed in the enumeration facet.. instance is invalid

I suspect this is a braino. How does one restrict xml with an XML Schema?

Thanks for your time and consideration.

Sincerely, -john

Here is the xsd from the tutorial. <xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element>

© Stack Overflow or respective owner

Related posts about qt

Related posts about xml-schema