tags:

views:

87

answers:

1

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?

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>
A: 

The problem had to do with whitespace. As formatted on this, it actuals works. However, if the start tag, value and end tag are on separate lines, it is invalid.

-jk

John