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

时间:2014-09-03 10:56:09

标签: java perl xhtml xslt-2.0 saxon

我正在尝试使用perl和XML :: Saxon :: XSLT2运行简单的XSLT 2.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>

xslttest.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
(...)

1 个答案:

答案 0 :(得分:1)

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

<?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
  • Inline :: Java 0.53
  • XML :: Saxon :: XSLT2 0.007

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

同样,您是否运行test case that comes with XML::Saxon::XSLT2?它通过了吗? (因为它与您的测试脚本非常相似。)