xml - 如何使用 saxon 内置目录功能

标签 xml validation dtd saxon xmlcatalog

我下载了 SaxonHE9-4-0-6J 并想在 CLI 上处理 XHTML。然而,Saxon 试图从 W3C 加载 DTD,并且每个简单的命令都需要花费太多时间。

我有 xml 目录,我通过设置指向目录文件的环境变量成功地将它与 xmllint 一起使用,但我不知道如何让 Saxon 使用它。谷歌揭示了关于使用 Saxon 目录的整个变化历史(因此困惑),没有一个让我高兴。

我下载了 resolver.jar 并将其设置在我的 CLASSPATH 中,但我无法让 Saxon 使用它。 各种组合后,我跟着http://www.saxonica.com/documentation/sourcedocs/xml-catalogs.xml通过仅使用目录变量,例如:

-catalog:path-to-my-catalog

(尝试了 URI 和常规路径),并且没有设置 -r-x-y 开关,但 Saxon 没有看不到它。我收到此错误:

Query processing failed: Failed to load Apache catalog resolver library

resolver.jar 设置在我的类路径中,我可以从命令行使用它:

C:\temp>java org.apache.xml.resolver.apps.resolver
Usage: resolver [options] keyword

Where:

-c catalogfile  Loads a particular catalog file.
-n name         Sets the name.
-p publicId     Sets the public identifier.
-s systemId     Sets the system identifier.
-a              Makes the system URI absolute before resolution
-u uri          Sets the URI.
-d integer      Set the debug level.
keyword         Identifies the type of resolution to perform:
                doctype, document, entity, notation, public, system,
                or uri.

OTOH,Saxon 存档本身已经包含 XHTML 和各种其他 DTD,因此必须有简单的方法来摆脱这种挫败感。

如何在命令行上使用 Saxon 并指示它使用本地 DTD?

最佳答案

从您问题中的 saxonica 链接:

When the -catalog option is used on the command line, this overrides the internal resolver used in Saxon (from 9.4) to redirect well-known W3C references (such as the XHTML DTD) to Saxon's local copies of these resources. Because both these features rely on setting the XML parser's EntityResolver, it is not possible to use them in conjunction.

这听起来像是 Saxon 自动使用 well-known W3C DTDs 的本地副本,但如果您指定 -catalog,它不会使用内部解析器,您必须在您的目录中明确指定这些。


这是一个在 Saxon 中使用目录的工作示例...

我的示例的文件/目录结构

C:/so_test/lib
C:/so_test/lib/catalog.xml
C:/so_test/lib/resolver.jar
C:/so_test/lib/saxon9he.jar
C:/so_test/lib/test.dtd
C:/so_test/test.xml

XML DTD (so_test/lib/test.dtd)

<!ELEMENT test (foo)>
<!ELEMENT foo (#PCDATA)>

XML 实例 (so_test/test.xml)

请注意,系统标识符指向一个不存在的位置,以确保正在使用该目录。

<!DOCTYPE test PUBLIC "-//TEST//Dan Test//EN" "dir_that_doesnt_exist/test.dtd">
<test>
    <foo>Success!</foo>
</test>

XML 目录 (so_test/lib/catalog.xml)

<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
    <group prefer="public" xml:base="file:///C:/so_test/lib">
        <public publicId="-//TEST//Dan Test//EN" uri="lib/test.dtd"/>
    </group>
</catalog>

命令行

注意 -dtd 选项以启用验证。

C:\so_test>java -cp lib/saxon9he.jar;lib/resolver.jar net.sf.saxon.Query -s:"test.xml" -qs:"<results>{data(/test/foo)}</results>" -catalog:"lib/catalog.xml" -dtd

结果

<results>Success!</results>

如果我使 XML 实例无效:

<!DOCTYPE test PUBLIC "-//TEST//Dan Test//EN" "dir_that_doesnt_exist/test.dtd">
<test>
    <x/>
    <foo>Success!</foo>
</test>

并运行与上面相同的命令行,结果如下:

Recoverable error on line 4 column 6 of test.xml:
  SXXP0003: Error reported by XML parser: Element type "x" must be declared.
Recoverable error on line 6 column 8 of test.xml:
  SXXP0003: Error reported by XML parser: The content of element type "test" must match "(foo)".
Query processing failed: The XML parser reported two validation errors

希望这个例子能帮助您弄清楚要更改您的设置的内容。

此外,使用 -t 选项可为您提供其他信息,例如加载了哪些目录以及是否解析了公共(public)标识符:

Loading catalog: file:///C:/so_test/lib/catalog.xml
Saxon-HE 9.4.0.6J from Saxonica
Java version 1.6.0_35
Analyzing query from {<results>{data(/test/foo)}</results>}
Analysis time: 122.70132 milliseconds
Processing file:/C:/so_test/test.xml
Using parser org.apache.xml.resolver.tools.ResolvingXMLReader
Building tree for file:/C:/so_test/test.xml using class net.sf.saxon.tree.tiny.TinyBuilder
Resolved public: -//TEST//Dan Test//EN
        file:/C:/so_test/lib/test.dtd
Tree built in 0 milliseconds
Tree size: 5 nodes, 8 characters, 0 attributes
<?xml version="1.0" encoding="UTF-8"?><results>Success!</results>Execution time: 19.482079ms
Memory used: 20648808

附加信息

Saxon distributes the Apache version of Xerces , 所以使用在 Apache Xerces distribution 中找到的 resolver.jar .

关于xml - 如何使用 saxon 内置目录功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14165765/

相关文章:

java - 手动更改进度条颜色

c# - XDocument 获取 XML 文件的一部分

javascript - 仅当表单已提交时才触发 jQuery 表单验证?

java - 如何正确使 dbunit 引用 xml 数据集中的 dtd 文件?

xml - 如何通过分层节点与 XSLT 循环?

xml - 将 Magento 前端选项卡修改为自定义 View : For bundled products

java - Spring集成验证

php form curse-word input filter解决方案

javascript - 根据 Javascript 中的 DTD 验证 XML?

java - struts-config.xml中action节点的 "attribute"属性有什么用?