xerces-c 解析xml schema

一:方法名方法说明

VoidSetDoSchema(constboolnewState)设置Parser是否处理xml文档中的schema,如果为true,则Parser还要处理xml中的schema,否则,parser不处理xml文档的schema。


VoidsetDoValidation(constboolnewState)此方法同setValidationScheme,不推荐使用。


VoidsetDoNamespaces(constboolnewState)设置Parser是否处理xml文档中的名域,如果为true,则Parser增强名域定义的约束和规则。


VoidsetValidationScheme(constValSchemesnewScheme)设置Parser利用定义的schema对xml文档进行有效性验证解析。NewScheme的值有Val_Never,Val_Auto,Val_Always,分别表示对xml文档不进行shema有效验证、自动选择是否验证、总是进行有效验证。

Usage

Here is an example how to turn on schema processing in DOMParser (default is off). Note that you must also turn on namespace support (default is off) for schema processing.

// Instantiate the DOM parser.
DOMParser parser;
parser.setDoNamespaces(true);
parser.setDoSchema(true);
parser.parse(xmlFile);

Usage in SAXParser is similar, please refer to the sample program 'samples/SAXCount/SAXCount.cpp' for further reference.

Here is an example how to turn on schema processing in SAX2XMLReader (default is on). Note that namespace must be on (default is on) as well.

SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
parser->setFeature(XMLUni::fgXercesSchema, true);
parser->parse(xmlFile);

Review the sample file, 'samples/data/personal-schema.xml' and 'samples/data/personal.xsd' for an example of an XML Schema grammar.


Associating Schema Grammar with instance document

Schema grammars can be associated with instance documents in two ways.

Specifying Schema Grammar through method calls:

An application developer may associate schemas with instance documents through methodssetExternalSchemaLocationif they use namespaces, andsetExternalNoNamespaceSchemaLocationotherwise. (For SAX2XMLReader, use the properties: "http://apache.org/xml/properties/schema/external-schemaLocation" and "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation")

Here is an example with no target namespace:

// Instantiate the DOM parser.
DOMParser parser;
parser.setDoNamespaces(true);
parser.setDoSchema(true);
parser.setExternalNoNamespaceSchemaLocation("personal.xsd");
parser.parse("test.xml");

// Instantiate the SAX2 XMLReader.
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
XMLCh* propertyValue = XMLString::transcode("personal.xsd");
ArrayJanitor<XMLCh> janValue(propertyValue);

parser->setProperty(
       XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
       propertyValue);
parser.parse("test.xml");

Here is an example with a target namespace. Note that it is an error to specify a different namespace insetExternalSchemaLocationthan the target namespace defined in the Schema.

// Instantiate the DOM parser.
DOMParser parser;
parser.setDoNamespaces(true);
parser.setDoSchema(true);
parser.setExternalSchemaLocation("http://my.com personal.xsd http://my2.com test2.xsd");
parser.parse("test.xml");

// Instantiate the SAX2 XMLReader.
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
XMLCh* propertyValue = XMLString::transcode("http://my.com personal.xsd http://my2.com test2.xsd");
ArrayJanitor<XMLCh> janValue(propertyValue);

parser->setProperty(
       XMLCh XMLUni::fgXercesSchemaExternalSchemaLocation,
       propertyValue);
parser.parse("test.xml");

Specifying Schema Grammar through attributes in the instance document:

If schema grammar was not specified externally through methods, then each instance document that uses XML Schema grammars must specify the location of the grammars it uses by using an xsi:schemaLocation attribute if they use namespaces, and xsi:noNamespaceSchemaLocation attribute otherwise.

Here is an example with no target namespace:

<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:noNamespaceSchemaLocation='personal.xsd'>
...
</personnel>

Here is an example with a target namespace. Note that it is an error to specify a different namespace in xsi:schemaLocation attribute than the target namespace defined in the Schema.

<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns="http://my.com"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://my.com personal.xsd http://my2.com test2.xsd">
...
</personnel>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值