执行xslt转换时出现Perl错误 [英] Perl Error while performing xslt transformation
问题描述
我正在尝试使用XSLT 1.0执行简单的xml转换. 这是我的xml和xslt文件.
I am trying to perform a simple xml transformation using XSLT 1.0. Here are my xml and xslt files.
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>
XSLT文件
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<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>
这是我正在使用的perl脚本.
And here is my perl script that I am using.
use strict;
use warnings;
use Getopt::Std;
use File::Path;
use File::Spec;
use File::Basename;
use Env;
use XML::LibXSLT;
use XML::LibXML;
my %opts = ();
getopts('p:f:'\%opts);
my $xsltfile = $opts{'p'};
die "XSLT file not specified" if !defined($xsltfile);
my $xmlfile = $opts{'f'};
die "XML file not specified" if !defined($xmlfile);
# XSLT Transformation code starts here
#my $xml_parser = XML::LibXML->new();
#my $source = $xml_parser->parse_file($msgcatfile);
my $source = XML::LibXML->load_xml(location => $xmlfile);
#my $xslt_parser = XML::LibXML->new();
#my $xslt_source = $xslt_parser->parse_file($xsltfile);
my $xslt_source = XML::LibXML->load_xml(location => $xsltfile);
my $xslt = XML::LibXSLT->new();
my $stylesheet;
eval { $stylesheet = $xslt->parse_stylesheet($xslt_source); };
if ($@)
{
print "$@";
die "\n!******************Error in parsing the stylesheet file : $xsltfile ************************!\n";
}
eval { my $results = $stylesheet->transform_file($source); };
if ($@)
{
print "$@";
die "\n!******************Error in transforming the input xml file : $source ************************!\n";
}
print $stylesheet->output_as_bytes($results);
0;
我不确定发生了什么问题,但是在运行此perl脚本时,我遇到了无法识别的错误.
I am not sure what is going wrong but when run this perl script, I am getting following errors which I am not able decipher.
Bareword found where operator expected at trans.xslt line 2, near ""1.0" xmlns"
(Missing operator before xmlns?)
Bareword found where operator expected at trans.xslt line 11, near "</xsl"
(Might be a runaway multi-line // string starting on line 10)
(Missing operator before l?)
syntax error at trans.xslt line 2, near "xsl:"
Execution of trans.xslt aborted due to compilation errors.
当我在错误消息中搜索关键字时,找不到任何类似的帖子(与XML/XSLT相关). 我缺少明显的东西吗?
I could not find any similar posts (relevant to XML/XSLT) when I searched for keywords in the error message. Am I missing something obvious?
:更新:
我以
perl transform.pl -p trans.xslt -f example.xml
推荐答案
您以某种方式将XSLT文件作为Perl代码执行,但是您的问题中没有任何内容可以解释如何执行.实际上,正如我所评论的那样,您显示的不能的Perl代码已引起您所说的错误,因为它无法编译
Somehow you are executing your XSLT file as Perl code, but there is nothing in your question to explain how. In fact, as I commented, the Perl code that you show cannot have caused the error you say it did because it won't compile
我可以看到对$stylesheet->transform_file($source)
的调用存在问题,该调用应该为$stylesheet->transform($source)
或$stylesheet->transform_file($xmlfile)
,但是其余的错误很明显
I can see a problem with the call to $stylesheet->transform_file($source)
, which should be either $stylesheet->transform($source)
or $stylesheet->transform_file($xmlfile)
, but the rest of the bugs are obvious
还请注意,使用xml-stylesheet
处理指令附加到XML文档的样式表是test.xsl
,而您的Perl代码则应用test.xslt
.您应该选择其中一个
Note also that the stylesheet attached to the XML document with the xml-stylesheet
processing instruction is test.xsl
, whereas your Perl code applies test.xslt
. You should choose one or the other
您对$stylesheet->output_as_bytes($results)
的呼叫比$stylesheet->output_as_chars($results)
更好.它与纯ASCII数据没有任何区别,但是前者将产生编码的八位位组,而这几乎没有用.通常你只想要一个字符串
Your call to $stylesheet->output_as_bytes($results)
is better as $stylesheet->output_as_chars($results)
. It doesn't make any difference with pure ASCII data, but the former will produce encoded octets, which is rarely useful. Usually you just want a character string
在基本程序正常运行之前,最好避免编写奇特的参数输入和异常处理代码.我建议您改从我的代码开始,并使用 Try::Tiny
模块而不是如果必须处理错误,则单击eval
.目前,您的所有处理人员似乎要做的就是用大量星星来补充异常消息,然后无论如何都会死亡,所以我认为您可以不用它们
It's best to avoid writing fancy parameter input and exception-handling code before you have the basic program working. I suggest you start from my code here instead, and use the Try::Tiny
module instead of a simple eval
if you must handle the errors. At present, all your handlers seem to do is supplement the exception message with a lot of stars and then die anyway, so I think you can do without them
use strict;
use warnings;
use XML::LibXSLT;
my ($xmlfile, $xsltfile) = qw/ example.xml trans.xsl /;
my $xslt = XML::LibXSLT->new;
my $stylesheet = $xslt->parse_stylesheet_file($xsltfile);
my $results = $stylesheet->transform_file($xmlfile);
print $stylesheet->output_as_chars($results);
输出
Article - My Article
Authors:
- Mr. Foo
- Mr. Bar
这篇关于执行xslt转换时出现Perl错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!