内容表

QXmlSchemaValidator

注意

该类在 Qt4.6 引入

概要

函数

详细描述

PySide.QtXmlPatterns.QXmlSchemaValidator class validates XML instance documents against a W3C XML Schema.

PySide.QtXmlPatterns.QXmlSchemaValidator class loads, parses an XML instance document and validates it against a W3C XML Schema that has been compiled with PySide.QtXmlPatterns.QXmlSchema .

The following example shows how to load a XML Schema from a local file, check whether it is a valid schema document and use it for validation of an XML instance document:

schemaUrl = QUrl("file:///home/user/schema.xsd")
schema = QXmlSchema()
schema.load(schemaUrl)
if schema.isValid():
    file = QFile("test.xml")
    file.open(QIODevice.ReadOnly)
    validator = QXmlSchemaValidator(schema)
    if validator.validate(file, QUrl.fromLocalFile(file.fileName())):
        print "instance document is valid"
    else:
        print "instance document is invalid"
}
									

XML 模式版本

This class implements schema validation according to the XML Schema 1.0 specification.

另请参阅

PySide.QtXmlPatterns.QXmlSchema XML 模式验证范例

class PySide.QtXmlPatterns. QXmlSchemaValidator
class PySide.QtXmlPatterns. QXmlSchemaValidator ( schema )
参数: schema PySide.QtXmlPatterns.QXmlSchema

Constructs a schema validator. The schema used for validation must be referenced in the XML instance document via the xsi:schemaLocation or xsi:noNamespaceSchemaLocation 属性。

Constructs a schema validator that will use schema for validation. If an empty PySide.QtXmlPatterns.QXmlSchema schema is passed to the validator, the schema used for validation must be referenced in the XML instance document via the xsi:schemaLocation or xsi:noNamespaceSchemaLocation 属性。

PySide.QtXmlPatterns.QXmlSchemaValidator. messageHandler ( )
返回类型: PySide.QtXmlPatterns.QAbstractMessageHandler

Returns the message handler that handles parsing and validation messages for this PySide.QtXmlPatterns.QXmlSchemaValidator .

PySide.QtXmlPatterns.QXmlSchemaValidator. namePool ( )
返回类型: PySide.QtXmlPatterns.QXmlNamePool

Returns the name pool used by this PySide.QtXmlPatterns.QXmlSchemaValidator for constructing names . There is no setter for the name pool, because mixing name pools causes errors due to name confusion.

PySide.QtXmlPatterns.QXmlSchemaValidator. networkAccessManager ( )
返回类型: PySide.QtNetwork.QNetworkAccessManager

Returns the network manager, or 0 if it has not been set.

PySide.QtXmlPatterns.QXmlSchemaValidator. schema ( )
返回类型: QXmlSchema*

Returns the schema that is used for validation.

PySide.QtXmlPatterns.QXmlSchemaValidator. setMessageHandler ( handler )
参数: handler PySide.QtXmlPatterns.QAbstractMessageHandler

改变 message handler for this PySide.QtXmlPatterns.QXmlSchemaValidator to handler . The schema validator sends all parsing and validation messages to this message handler. PySide.QtXmlPatterns.QXmlSchemaValidator 未拥有所有权对于 handler .

Normally, the default message handler is sufficient. It writes compile and validation messages to stderr . The default message handler includes color codes if stderr can render colors.

PySide.QtXmlPatterns.QXmlSchemaValidator calls QAbstractMessageHandler.message() , the arguments are as follows:

message() argument Semantics
QtMsgType type Only QtWarningMsg and QtFatalMsg are used. The former identifies a warning, while the latter identifies an error.
const PySide.QtCore.QString & description An XHTML document which is the actual message. It is translated into the current language.
const PySide.QtCore.QUrl &identifier Identifies the error with a URI, where the fragment is the error code, and the rest of the URI is the error namespace.
const PySide.QtXmlPatterns.QSourceLocation & sourceLocation Identifies where the error occurred.
PySide.QtXmlPatterns.QXmlSchemaValidator. setNetworkAccessManager ( networkmanager )
参数: networkmanager PySide.QtNetwork.QNetworkAccessManager

将网络管理器设为 manager . PySide.QtXmlPatterns.QXmlSchemaValidator 未拥有所有权对于 manager .

PySide.QtXmlPatterns.QXmlSchemaValidator. setSchema ( schema )
参数: schema PySide.QtXmlPatterns.QXmlSchema

设置 schema that shall be used for further validation. If the schema is empty, the schema used for validation must be referenced in the XML instance document via the xsi:schemaLocation or xsi:noNamespaceSchemaLocation 属性。

PySide.QtXmlPatterns.QXmlSchemaValidator. setUriResolver ( resolver )
参数: resolver PySide.QtXmlPatterns.QAbstractUriResolver

将 URI 解析器设为 resolver . PySide.QtXmlPatterns.QXmlSchemaValidator 未拥有所有权对于 resolver .

PySide.QtXmlPatterns.QXmlSchemaValidator. uriResolver ( )
返回类型: PySide.QtXmlPatterns.QAbstractUriResolver

Returns the schema's URI resolver. If no URI resolver has been set, QtXmlPatterns will use the URIs in instance documents as they are.

The URI resolver provides a level of abstraction, or polymorphic URIs . A resolver can rewrite logical URIs to physical ones, or it can translate obsolete or invalid URIs to valid ones.

QtXmlPatterns calls QAbstractUriResolver.resolve() the absolute URI is the URI mandated by the schema specification, and the relative URI is the URI specified by the user.

PySide.QtXmlPatterns.QXmlSchemaValidator. validate ( source [ , documentUri=QUrl() ] )
参数:
返回类型:

PySide.QtCore.bool

Validates the XML instance document read from source 采用给定 documentUri against the schema.

返回 true if the XML instance document is valid according to the schema, false 否则。

范例:

schema = getSchema()
file = QFile("test.xml")
file.open(QIODevice.ReadOnly)
validator = QXmlSchemaValidator(schema)
if validator.validate(file, QUrl.fromLocalFile(file.fileName())):
    print "instance document is valid"
else:
    print "instance document is invalid"
												
PySide.QtXmlPatterns.QXmlSchemaValidator. validate ( data [ , documentUri=QUrl() ] )
参数:
返回类型:

PySide.QtCore.bool

Validates the XML instance document read from data 采用给定 documentUri against the schema.

返回 true if the XML instance document is valid according to the schema, false 否则。

范例:

schema = getSchema()
data = QByteArray("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test></test>")
buffer = QBuffer(data)
buffer.open(QIODevice.ReadOnly)
QXmlSchemaValidator validator(schema)
if validator.validate(buffer):
    print "instance document is valid"
else:
    print "instance document is invalid"
											
PySide.QtXmlPatterns.QXmlSchemaValidator. validate ( source )
参数: source PySide.QtCore.QUrl
返回类型: PySide.QtCore.bool

Validates the XML instance document read from source against the schema.

返回 true if the XML instance document is valid according to the schema, false 否则。

范例:

schema = getSchema()
url = QUrl("http://www.schema-example.org/test.xml")
validator = QXmlSchemaValidator(schema)
if validator.validate(url):
    print "instance document is valid"
else:
    print "instance document is invalid"