【问题标题】:How to set up catalog files for xmllint?如何为 xmllint 设置目录文件?
【发布时间】:2012-07-22 07:53:10
【问题描述】:

好的。我想为 xmllint 设置目录文件来修复问题,以便从本地文档验证 dcterms xml 命名空间。我相信我所做的一切都是正确的,但它似乎根本不起作用。

我正在运行 OSX。

我已经创建了一个目录 /etc/xml

$ mkdir /etc/xml
$ cd /etc/xml

我已将 dcterms.xsd 下载到该目录

$ ls -l
-rw-r--r--  1 ibis  wheel  12507 24 Jul 11:42 dcterms.xsd

我创建了一个名为“目录”的文件

$ xmlcatalog --create > catalog

我已将 dcterms 命名空间添加到目录文件中

$ xmlcatalog --noout --add uri http://purl.org/dc/elements/1.1/ file:///etc/xml/dc.xsd
$ xmlcatalog --noout --add uri http://purl.org/dc/terms/ file:///etc/xml/dcterms.xsd
$ cat catalog
<?xml version="1.0"?>
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
  <uri name="http://purl.org/dc/elements/1.1/" uri="file:///etc/xml/dc.xsd"/>
  <uri name="http://purl.org/dc/terms/" uri="file:///etc/xml/dcterms.xsd"/>
</catalog>

在工作目录中,我创建了一个名为 Empty.xsd 的简单 xml 架构

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Empty" xmlns:tns="http://www.example.org/Empty" elementFormDefault="qualified">
  <element name="empty">
    <complexType>
      <sequence>
        <any processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
      </sequence>
      <anyAttribute></anyAttribute>
    </complexType>
  </element>
</schema>

注意 processcontnts 是“严格的”。

我创建了一个应该触发所有验证的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<empty xmlns="http://www.example.org/Empty" 
          xmlns:dcterms="http://purl.org/dc/terms/">
    <dcterms:title>A title</dcterms:title>
</empty>

然后我尝试验证它。

$ xmllint --noout --valid --schema Empty.xsd Empty.xml
Empty.xml:2: validity error : Validation failed: no DTD found !
y xmlns="http://www.example.org/Empty" xmlns:dcterms="http://purl.org/dc/terms/"
                                                                               ^
Empty.xml:3: element title: Schemas validity error : Element '{http://purl.org/dc/terms/}title': No matching global element declaration available, but demanded by the strict wildcard.
Empty.xml fails to validate

我已按照文档中的说明设置了一个目录,并将其指向本地 dcterms 架构文件。为什么xmllint找不到呢?

【问题讨论】:

    标签: xml xsd xml-validation lint


    【解决方案1】:

    程序 xmllint 不会根据待解析 XML 文件中的 xmlns="something" 属性自动加载 XSD 文件,它只使用在 --schema 参数中指定的 XSD(以及导入的那些) /包括在内)。

    对于测试,您可以像这样创建NonEmpty.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <schema 
        xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.org/Empty"
        elementFormDefault="qualified">
      <include schemaLocation="Empty.xsd"/>
      <import schemaLocation="dcterms.xsd" namespace="http://purl.org/dc/terms/"/>
    </schema>
    

    用法:

    $ xmllint -debugent -noout -schema NonEmpty.xsd Empty.xml
    new input from file: NonEmpty.xsd
    new input from file: Empty.xsd
    new input from file: dcterms.xsd
    new input from file: http://www.w3.org/2001/03/xml.xsd
    new input from file: dc.xsd
    new input from file: dcmitype.xsd
    new input from file: Empty.xml
    Empty.xml validates
    

    现在有了catalog 文件:

    <?xml version="1.0"?>
    <!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
    <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
      <uri name="http://www.w3.org/2001/03/xml.xsd"          uri="file:///home/zsiga/proba/dcterms/2001_03_xml.xsd"/>
      <uri name="http://dublincore.org/schemas/xmls/qdc/dcterms.xsd" uri="file:///home/zsiga/proba/dcterms/dcterms.xsd"/>
    </catalog>
    

    这是NonEmpty2.xsd 文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <schema 
        xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.org/Empty"
        elementFormDefault="qualified">
      <include schemaLocation="Empty.xsd"/>
      <import schemaLocation="http://dublincore.org/schemas/xmls/qdc/dcterms.xsd" namespace="http://purl.org/dc/terms/"/>
    </schema>
    

    及其用法:

    $ XML_CATALOG_FILES=./catalog xmllint -debugent -noout \
        -schema NonEmpty2.xsd Empty.xml
    new input from file: NonEmpty2.xsd
    new input from file: Empty.xsd
    new input from file: file:///home/zsiga/proba/dcterms/dcterms.xsd
    new input from file: file:///home/zsiga/proba/dcterms/2001_03_xml.xsd
    new input from file: file:///home/zsiga/proba/dcterms/dc.xsd
    new input from file: file:///home/zsiga/proba/dcterms/dcmitype.xsd
    new input from file: Empty.xml
    Empty.xml validates
    

    --- 编辑 2020.11.02。 ---

    我建议在catalog 中使用&lt;systemId&gt; 标记,同时使用相对路径名:

    <?xml version="1.0"?>
    <!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
    <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
      <system systemId="http://www.w3.org/2001/03/xml.xsd"                  uri="2001_03_xml.xsd"/>
      <system systemId="http://dublincore.org/schemas/xmls/qdc/dcterms.xsd" uri="dcterms.xsd"/>
    </catalog>
    

    结果是一样的,但有些程序更喜欢&lt;system&gt; 而不是&lt;uri&gt;。相对路径名 [相对于catalog 文件的位置] 可能更容易处理。

    【讨论】:

      【解决方案2】:
      1. 我在dcterms 中没有title 元素,所以我将其替换为abstract
      2. 我找不到任何确认,但其他人也报告了在 libxml 中为 xsd 模式使用目录文件的问题。不过,我发现目录对 dtds 工作正常。
      3. 有一个解决方法。将&lt;import namespace="http://purl.org/dc/terms/" schemaLocation="dcterms.xsd" /&gt; 插入Empty.xsd。之后我删除了No matching global 消息。
      4. No DTD found 仍然可见,但返回码从 3 增加到 4,这意味着解析成功。
      5. 编辑:--sax 开关似乎有助于“未找到 DTD”消息。

      相关问题:Xml validation with schema header and Catalog lookup,没有答案。这就是第 2 点。

      【讨论】:

        猜你喜欢
        • 2017-02-16
        • 1970-01-01
        • 2022-06-21
        • 2020-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-26
        相关资源
        最近更新 更多