【问题标题】:Using Schematron with Perl将 Schematron 与 Perl 一起使用
【发布时间】:2013-07-23 00:59:38
【问题描述】:

我正在尝试阅读这篇关于 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 的新手。

【问题讨论】:

    标签: xml perl schematron docbook-5


    【解决方案1】:

    这看起来像是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 模式中声明的任何命名空间。

    【讨论】:

      猜你喜欢
      • 2014-03-30
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 2020-03-23
      相关资源
      最近更新 更多