2014-09-02 28 views
1

我有一個小項目(在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,我會非常高興!

回答

1

第一組錯誤消息是抱怨解析器無法抓取下載由其URL指定的外部XSD文件。

你正在運行這個能夠訪問互聯網的盒子嗎?

爲了能夠導入外部XSD文件,自然必須具有Internet訪問權限。

在缺乏Internet訪問的情況下,可以手動下載任何需要的外部文件,並通過xml目錄文件在本地加載它們;但這是一個不同的,更復雜的話題。