Perl Error while performing xslt transformation

前端 未结 2 728
悲哀的现实
悲哀的现实 2020-12-12 06:45

I am trying to perform a simple xml transformation using XSLT 1.0. Here are my xml and xslt files.

XML File



        
相关标签:
2条回答
  • 2020-12-12 06:56

    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

    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

    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

    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

    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);
    

    output

    Article - My Article
    Authors: 
    - Mr. Foo
    - Mr. Bar
    
    0 讨论(0)
  • 2020-12-12 07:10

    Execution of trans.xslt aborted due to compilation errors.

    This looks like you're trying to execute your XSLT file, not your Perl program.

    You should be running something like this:

    $ ./your_xslt_processor.pl -f your_xml.xml -p trans.xslt
    
    0 讨论(0)
提交回复
热议问题