开发者

How does one restrict xml with an XML Schema?

开发者 https://www.devze.com 2023-01-02 03:24 出处:网络
I want to restrict xml with a schema to a specific set. I read this tutorial http://www.w3schools.com/schema/schema_facets.asp

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 invali开发者_如何转开发d";
    }
} 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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消