使用XML::Saxon::XSLT2转换XHTML



我正在尝试使用perl和XML::Saxon::XSLT2运行一个简单的XSLT2.0转换。以下是我迄今为止所尝试的:

test.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My Title</title>
    </head>
    <body>
        <p>My Content</p>
    </body>
</html>

test.xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="2.0">
    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

xsltest.pl:

use strictures;
use diagnostics;
use XML::Saxon::XSLT2;
use XML::LibXML;
my $parser = XML::LibXML->new;
my $xsl = 'test.xslt';
my $xslt = $parser->load_xml( location => $xsl );
my $dom = $parser->load_xml( location => 'test.xhtml' );
my $trans = XML::Saxon::XSLT2->new($xslt);
my $output = $trans->transform( $dom, 'xhtml' );
print $output;

locate saxon9he.jar:输出

/usr/local/share/java/saxon9he.jar
/usr/share/java/saxon9he.jar

现在,如果我运行perl xslttest.pl,我得到:

Uncaught exception from user code:
    A problem was encountered while attempting to compile and install your Inline
    Java code. The command that failed was:
      "/usr/lib/jvm/default-java/bin/javac" -deprecation  -d "/tmp/perltest/_Inline/lib/auto/XML/Saxon/XSLT2_dbc0" Transformer.java > cmd.out 2>&1
    The build directory was:
    /tmp/perltest/_Inline/build/XML/Saxon/XSLT2_dbc0
    The error message was:
    Transformer.java:1: error: package net.sf.saxon.s9api does not exist
    import net.sf.saxon.s9api.*;
    ^
    Transformer.java:29: error: cannot find symbol
        private XsltExecutable xslt;
                ^
      symbol:   class XsltExecutable
      location: class Transformer
    Transformer.java:30: error: cannot find symbol
        private Processor proc;
                ^
      symbol:   class Processor
      location: class Transformer
    Transformer.java:31: error: cannot find symbol
        private HashMap<String, XdmAtomicValue> params;
                                ^
    (...)

Perl版本(perl -v的结果):

This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-linux-gnu-thread-multi
(with 41 registered patches, see perl -V for more detail)
(...)

为什么转型失败了?Inline::Java似乎无法编译所需的代码(???),但如何避免这种情况?

编辑:

net.sf.saxon.s9api.*类由saxon9he.jar本身提供:

$ jar tvf /usr/share/java/saxon9he.jar | grep net.sf.saxon.s9api
     0 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/
  1805 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/Axis.class
   302 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/BuildingContentHandler.class
   421 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/BuildingStreamWriter.class
  1207 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/BuildingStreamWriterImpl.class
  2025 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/ConstructedItemType.class
  1130 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/DOMDestination.class
   303 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/Destination.class
  1367 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/DocumentBuilder$BuildingContentHandlerImpl.class
  8981 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/DocumentBuilder.class
   455 Wed Aug 06 10:34:58 CEST 2014 net/sf/saxon/s9api/ExtensionFunction.class
(...)

您的脚本适用于我,并产生以下输出:

<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>My Title</title>
   </head>
   <body>
      <p>My Content</p>
   </body>
</html>

(虽然不是很快。我忘了Inline::Java的东西有多慢了。)

我安装了以下软件(在Ubuntu 14.04上):

  • Perl 5.20
  • OpenJDK运行时环境(IcedTea 2.5.1)(7u65-2.5.1-4ubuntu1~0.14.04.2),安装在/usr/lib/jvm/java-7-openjdk-i386/
  • Saxon HE 9.5.1.7,位于/usr/share/java/saxon9he.jar
  • 内联::Java 0.53
  • XML::Saxon::XSLT2 0.007

你们有类似的版本吗?是否正确安装了Inline::Java(需要设置JAVA_HOME环境变量)。你做了测试吗?他们通过了吗?(如果您使用CPAN客户端安装Inline::Java,它通常会运行测试用例,并且只有在通过时才安装模块,除非您明确地覆盖此行为。)

类似地,您是否运行了XML::Saxon::XSLT2附带的测试用例?它通过了吗?(因为它做的事情与您的测试脚本非常相似。)

最新更新