Libxml2 xmlSchemaParse失败

问题描述:

我有一个小项目(在c + + Linux的Ubuntu 14.04),我试图解析一些XML文档使用libxml2。当我得到.xml文件时,我试图验证它。但有一些令人讨厌的错误!

我在验证过程中发现了有关使用少量.xsd模式的信息。为此,使用具有“schemaLocation”元素的“导入”元素(针对每个.xsd模式)创建.xsd文档非常重要。
还有就是我的.xsd架构:Libxml2 xmlSchemaParse失败

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns="http://osll.converter-schema" 
    targetNamespace="http://osll.converter-schema" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:OAI-PMH="http://www.openarchives.org/OAI/2.0" 
    xmlns:lido="http://www.lido-schema.org" 
    version="1.0" elementFormDefault="qualified" attributeFormDefault="unqualified"> 

    <xs:import namespace="http://www.openarchives.org/OAI/2.0" schemaLocation="http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"/> 
    <xs:import namespace="http://www.lido-schema.org" schemaLocation="http://www.lido-schema.org/schema/v1.0/lido-v1.0.xsd"/> 
</xs:schema> 

有C++使用的libxml2解析的.xsd架构代码:

bool XmlDocument::validate(const char* fileSchema) { 
    std::cout << "Starting validate xml-document.."; 
    xmlSchemaParserCtxtPtr schemaParser = xmlSchemaNewParserCtxt(fileSchema); 
    xmlSchemaPtr schema = xmlSchemaParse(schemaParser); 
    xmlSchemaValidCtxtPtr schemaValid = xmlSchemaNewValidCtxt(schema); 
    int result = xmlSchemaValidateDoc(schemaValid, xmlDocument); 
    if(result!=0) { std::cout << "Error! Code: " << result << std::endl; return false; } 
    else { std::cout << "Done!\n"; return true; } 
    return false; 
} 

,最后有错误的列表:

http://www.w3.org/1999/xlink.xsd:27:元素导入:模式解析器警告:元素'{http://www.w3.org/2001/XMLSchema}导入':跳过位于'的模式导入'命名空间'http://www.w3.org/XML/1998/namespace',因为这个命名空间已经被导入,并且模式位于'http://www.w3.org/2001/03/xml.xsd'。
错误:操作正在进行
I/O警告:未能加载外部实体“http://schemas.opengis.net/gml/3.1.1/base/coverage.xsd
http://schemas.opengis.net/gml/3.1.1/base/gml.xsd:16:元件包括:数据模式分析器错误:元素“{http://www.w3.org/2001/XMLSchema}包括”:未能加载文档“http://schemas.opengis.net/gml/3.1.1/base/coverage.xsd”为包容性。 错误:操作正在进行
I/O警告:未能加载外部实体 “http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd
response.xml:3:元素OAI-PMH:架构有效性警告:元素 '{} http://www.openarchives.org/OAI/2.0/ OAI-PMH',属性' {http://www.w3.org/2001/XMLSchema-instance} schemaLocation':位置'http://www.openarchives.org/OAI/2.0/OAI-PMH处的文档。 xsd'无法获得。
response.xml:3:element OAI-PMH:模式有效性错误:元素'OAI-PMH':没有匹配的全局声明可用于验证根目录。

请帮忙找到bug,我会非常高兴!

第一组错误消息是抱怨解析器无法抓取下载由其URL指定的外部XSD文件。

你正在运行这个能够访问互联网的盒子吗?

为了能够导入外部XSD文件,自然必须具有Internet访问权限。

在缺乏Internet访问的情况下,可以手动下载任何需要的外部文件,并通过xml目录文件在本地加载它们;但这是一个不同的,更复杂的话题。