好的。我希望为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>注意,处理内容是“严格的”。
我已经创建了一个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找不到它?
发布于 2018-10-11 13:28:12
程序xmllint不基于待解析XML-文件中的xmlns="something"属性自动加载XSD -文件,它只使用--schema参数中指定的XSD(以及从--schema参数中导入/包含的文件)。
对于测试,您可以创建如下所示的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
我建议在<systemId>中使用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">
<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>结果是一样的,但是有些程序更喜欢<system>而不是<uri>。而且,相对于catalog文件位置的相对路径名可能更容易处理。
发布于 2012-09-05 16:57:17
title中没有dcterms元素,所以我用abstract替换了它<import namespace="http://purl.org/dc/terms/" schemaLocation="dcterms.xsd" />插入Empty.xsd。从那以后,我就处理掉了No matching global消息。No DTD found仍然是可见的,但是返回代码从3增加到4,这意味着解析是成功的。--sax开关似乎有助于“没有DTD找到”消息。相关问题:Xml validation with schema header and Catalog lookup,没有答案。那大约是第2点。
https://stackoverflow.com/questions/11623369
复制相似问题