本文介绍了使用 XSD、目录解析器和用于 XSLT 的 JAXP DOM 验证

问题描述

背景

使用 JDK 6 将 DOM.

我的理解是 可用于配置此类验证.这是通过使用 带有 有关使用源自目录的 XSD 验证

这些问题和答案中的大多数引用了硬编码的 XSD 文件路径,或者使用 执行验证,或与 ,或需要 JDOM 依赖项,或者没有 的,但没有完整的、独立的编译和运行示例(在 JDK 6 下).

我发布了一个从技术上讲似乎有效但过于冗长的答案.

问题

验证和转换

  1. 创建目录解析器.
  2. 创建一个
  3. 将解析器与解析器相关联.
  4. 解析包含 XSD 引用的
  5. 在验证错误时终止.
  6. 使用 XSL 模板转换经过验证的

源文件

源文件包括目录管理器属性文件、Java 源代码、目录文件、./).

目录管理器属性文件

这个属性文件由 CatalogResolver 类读取;另存为 ./CatalogManager.properties:

catalogs=catalog.

TestXSD.java

这是主要的应用程序;将其另存为 ./src/TestXSD.java:

package src;导入 java.io.*;导入 java.net.URI;导入 java.util.*;导入 java.util.regex.Pattern;导入 java.util.regex.Matcher;导入 javax.= 版本;}/*** 为 XSLT2 文件启用 Saxon9 的 XSLT2 转换器.*/私有静态无效 useXSLT2Transformer() {System.setProperty("javax.

错误处理程序

这是人性化的错误信息的代码;另存为 ./src/DocumentErrorHandler.java:

package src;导入 java.io.PrintStream;导入 org.

目录文件

另存为./catalog.:

<下一个目录目录="./ArbitraryFolder/catalog.

不同的测试用例包括在处理指令或根节点中引用的 XSD.

Schema:处理指令

可以使用 处理指令 (PI) 提供模式.另存为 ./Tests/good-notes2.:

架构:根节点

可以在文档根节点的属性中提供架构.另存为 ./Tests/good-notes3.:

验证失败

以下应该无法通过验证(日期需要连字符);另存为 ./Tests/bad-note1.:

转型

另存为./Tests/note-to-html.xsl:

任意文件夹

任意文件夹表示计算机上文件的路径,这些文件可以位于文件系统的任何位置.这些文件的位置可能不同,例如,在生产、开发和存储库之间.

目录

将此文件另存为./ArbitraryFolder/catalog.:

注意事项

本示例中有两个文件用于转换笔记:notes.xsl 和 note-body.xsl.第一个包括第二个.

注释样式表

另存为 ./ArbitraryFolder/XSL/notes/notes.xsl:

<xsl:import href="note-body.xsl"/><xsl:template match="/"></xsl:模板><xsl:template match="note"><div><xsl:apply-templates select="title, date, body"/>

</xsl:模板><xsl:template match="title"><h1><xsl:value-of select="."/></h1></xsl:模板><xsl:模板匹配=日期"><p class="date"><xsl:value-of select="."/></p></xsl:模板></xsl:stylesheet>

注释正文样式表

另存为./ArbitraryFolder/XSL/notes/note-body.xsl:

架构

所需的最后一个文件是架构;将其另存为 ./schemas/notes/notes.xsd:

建筑

本节详细介绍了如何构建测试应用程序.

图书馆

您将需要 Saxon 9(用于 XSLT2.0 文档)、Xerces、Xalan 和 Resolver API:

jaxen-1.1.6.jar解析器.jar撒克逊9he.jar序列化器.jarxalan.jarxercesImpl.jar

脚本

另存为./build.sh:

#!/bin/bashjavac -d bin -cp .:lib/* src/TestXSD.java

另存为./run.sh:

#!/bin/bashjava -cp .:bin:lib/* src.TestXSD 测试/note-to-html.xsl $1

编译

使用./build.sh编译代码.

运行输出

运行使用:

./run.sh 文件名.

良好的测试

测试好笔记是否通过验证:

./run.sh 测试/good-note2.

没有错误.

糟糕的测试

测试坏笔记的日期没有通过验证:

./run.sh 测试/bad-note1.

正如预期的那样,这会产生所需的错误:

线程main" org.

Background

Using JDK 6 to load DOM. The

My understanding is that a can be used to configure such validation. This is achieved by using a with an Questions about validating

Most of these questions and answers reference a hard-coded XSD file path, or use to perform the validation, or pertain to , or require JDOM dependencies, or have no of , but no complete, standalone example that compiles and runs (under JDK 6).

I posted an answer that seems to work, technically, but is overly verbose.

Question

What is the canonical way (using JDK 1.6 libraries) to validate and transform an

  1. Create a catalog resolver.
  2. Create an
  3. Associate the resolver with the parser.
  4. Parse an
  5. Terminate on validation errors.
  6. Transform the validated

Source Files

The source files include a catalog manager properties file, Java source code, catalog file, ./).

Catalog Manager Properties File

This properties file is read by the CatalogResolver class; save as ./CatalogManager.properties:

catalogs=catalog.

TestXSD.java

This is the main application; save it as ./src/TestXSD.java:

package src;

import java.io.*;
import java.net.URI;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import javax.

Error Handler

This is the code for human-friendly error messages; save as ./src/DocumentErrorHandler.java:

package src;

import java.io.PrintStream;

import org.

Catalog File

Save as ./catalog.:

<?

The different test cases include XSDs referenced in either processing instructions or root nodes.

Schema: Processing Instruction

The schema can be provided using an processing instruction (PI). Save as ./Tests/good-notes2.:

<?

Schema: Root Node

The schema can be provided in attributes of the document's root node. Save as ./Tests/good-notes3.:

<?

Fail Validation

The following should fail validation (date needs hyphens); save as ./Tests/bad-note1.:

<?

Transformation

Save this as ./Tests/note-to-html.xsl:

<?

Arbitrary Folder

The arbitrary folder represents the path to files on a computer that can be located anywhere on the file system. The location of these files could differ, for example, between production, development, and the repository.

Catalog

Save this file as ./ArbitraryFolder/catalog.:

<?

Notes

There are two files in this example for transforming the notes: notes.xsl and note-body.xsl. The first includes the second.

Notes Stylesheet

Save this as ./ArbitraryFolder/XSL/notes/notes.xsl:

<?

Note Body Stylesheet

Save this as ./ArbitraryFolder/XSL/notes/note-body.xsl:

<?

Schema

The last file required is the schema; save this as ./schemas/notes/notes.xsd:

<?

Building

This section details how to build the test application.

Libraries

You will need Saxon 9 (for XSLT2.0 documents), Xerces, Xalan, and the Resolver API:

jaxen-1.1.6.jar
resolver.jar
saxon9he.jar
serializer.jar
xalan.jar
xercesImpl.jar

Scripts

Save as ./build.sh:

#!/bin/bash
javac -d bin -cp .:lib/* src/TestXSD.java

Save as ./run.sh:

#!/bin/bash
java -cp .:bin:lib/* src.TestXSD Tests/note-to-html.xsl $1

Compile

Use the ./build.sh to compile the code.

Run Output

Run using:

./run.sh filename.

Good Test

Test that the good note passes validation:

./run.sh Tests/good-note2.

No errors.

Bad Test

Test that the bad note's date does not pass validation:

./run.sh Tests/bad-note1.

As expected, this produces the desired error:

Exception in thread "main" org.

这篇关于使用 XSD、目录解析器和用于 XSLT 的 JAXP DOM 验证