1

我正在尝试阅读有关 Schematron 和 Perl 的文章。

http://www.xml.com/pub/a/2002/01/23/perl-schematron.html?page=2

我的目标是使用 Schematron 和 RelaxNG 来验证 Docbook 5 文档。

但是,我似乎无法让这个基本示例起作用。这是我从 O'Reilly 文章中复制的 Perl 脚本:

#!/usr/bin/perl -w
use strict;
use XML::Schematron::LibXSLT;


my $schema_file = $ARGV[0];
my $xml_file    = $ARGV[1];

die "Usage: perl schematron.pl schemafile XMLfile.\n"
    unless defined $schema_file and defined $xml_file;

my $tron = XML::Schematron::LibXSLT->new();

$tron->schema($schema_file);
my $ret = $tron->verify($xml_file);

print $ret . "\n";

我这样调用脚本:

perl schematron.pl path-to-docbook.sch path-to-xml-file

我得到了与命名空间“db”有关的错误:

xsltCompileStepPattern : no namespace bound to prefix db
compilation error: file unknown-101d82900 element template
xsltCompilePattern : failed to compile 'db:sidebar'
error
...

我的 docbook.sch 文件开始如下。它是 Docbook 5 发行版附带的 docbook.sch 模式文件:

<?xml version="1.0" encoding="utf-8"?>
<s:schema xmlns:s="http://www.ascc.net/xml/schematron" xmlns:db="http://docbook.org/ns/docbook">
  <s:ns prefix="db" uri="http://docbook.org/ns/docbook"/>
  <s:pattern name="Element exclusion">
    <s:rule context="db:sidebar">

会不会是 Perl 模块使用了不理解命名空间的 Schematron 版本?或者可能是 docbook.sch 不正确?那会很奇怪,因为它随 Docbook 发行版一起提供。

我可能缺少一些基本的东西,因为我是 Schematron 的新手。

4

1 回答 1

0

这看起来像XML::Schematron中的错误。没有考虑 docbook.sch 中的以下行:

<s:ns prefix="db" uri="http://docbook.org/ns/docbook"/> 

验证是通过将模式转换为 XSLT 样式表来完成的。通过在 XSLTProcessor.pm 模块中添加 DocBook 命名空间声明,我能够消除错误:

$self->append_template(
       qq|<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
       <xsl:stylesheet $ns version="1.0"
            xmlns:db="http://docbook.org/ns/docbook">   
       <xsl:output method="text"/>
       <xsl:template match="/">
       <xsl:apply-templates select="/" mode="$mode"/>|
   );

以上只是一个hack。正确的修复(我没有尝试过)当然应该适用于在 Schematron 模式中声明的任何命名空间。

于 2013-08-25T18:00:05.973 回答