【问题标题】:Libxml2 xmlSchemaParse failedLibxml2 xmlSchemaParse 失败
【发布时间】:2014-09-02 19:09:28
【问题描述】:

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

我发现了有关在验证期间使用一些 .xsd 模式的信息。为此,重要的是使用具有“schemaLocation”元素的“import”元素(对于每个 .xsd 架构)创建 .xsd 文档。
有我的 .xsd 架构:

<?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>

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

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}import”:为命名空间“http://www.w3.org/XML/1998/namespace”跳过位于“http://www.w3.org/2001/xml.xsd”的模式导入,因为该命名空间已被导入架构位于“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}include”:无法加载文档“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:元素 OAI-PMH:架构有效性错误:元素“{http://www.openarchives.org/OAI/2.0/}OAI-PMH”:没有可用于验证根的匹配全局声明。

请帮忙找bug,我会很高兴的!

【问题讨论】:

    标签: c++ linux xml-parsing libxml2 xsd-validation


    【解决方案1】:

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

    您运行它的机器是否可以访问互联网?

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-10
      • 2019-08-06
      • 2015-12-28
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多