Perl xslt转换输出到文件

时间:2016-06-27 11:51:22

标签: xml perl xslt libxslt

我正在使用此this答案来执行XSLT 1.0转换。我打算将输出保存在另一个xml文件中。因此,我补充说,

use strict;
use warnings;

use XML::LibXSLT;

my ($xmlfile, $xsltfile,$outfile) = qw/ example.xml trans.xsl out.xml /;

my $xslt = XML::LibXSLT->new;
my $stylesheet = $xslt->parse_stylesheet_file($xsltfile);
my $results    = $stylesheet->transform_file($xmlfile);

$stylesheet->output_file($results,$outfile);

这会产生以下错误,

Can't coerce UNKNOWN to string in entersub at $LongPath/XML/LibXSLT.pm line 485.

在线查询,我找到了blog,提到了类似的内容。

我错过了一些明显的东西吗?

更新

XML文件

<?xml version="1.0"?>
<?xml-stylesheet type="xsl" href="trans.xsl"?>
<Article>
  <Title>My Article</Title>
  <Authors>
    <Author>Mr. Foo</Author>
    <Author>Mr. Bar</Author>
  </Authors>
  <Body>This is my article text.</Body>
</Article>

XSL文件

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    
  <xsl:output method="xml"/>    
  <xsl:template match="/">
    Article - <xsl:value-of select="/Article/Title"/>
    Authors: <xsl:apply-templates select="/Article/Authors/Author"/>
  </xsl:template>    
  <xsl:template match="Author">
    - <xsl:value-of select="." />
  </xsl:template>    
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

我最终自己找到了问题。看来

$stylesheet->output_file($results,$outfile);

语句无法使用提供的路径创建文件。 此路径几乎没有不存在的目录。因此我最终做了

my $dir = dirname($outfile);
mkpath($dir);

之后我能够将输出保存到$outfile

相关问题