执行 xslt 转换时 Perl 错误 [英] Perl Error while performing xslt transformation

查看:18
本文介绍了执行 xslt 转换时 Perl 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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 "
!******************Error in parsing the stylesheet file : $xsltfile ************************!
";
}

eval { my $results = $stylesheet->transform_file($source); };

if ($@)
{
    print "$@";
    die "
!******************Error in transforming the input xml file : $source ************************!
";
}
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆