我有一个简单的perl程序,可以读取xml文件和xslt文件,并像这样对它运行转换
命令:
perl perl_xslt_translator.pl C000143.xml 04_TestStylesheet.xsl C000143_04.xml PROG123
perl代码如下所示。
代码:
use strict;
use warnings;
use Data::Dump qw(dump);
use XML::LibXML;
use XML::Simple;
use XML::LibXSLT;
use XML::LibXML;
use Data::Dumper;
print "Perl Starting ... \n\n";
#
# This reads the filename as an argument
#
# store the 1st argument into the variable
my ($XML_FILE, $XSLT_FILE, $OUTPUT_FILE, $PROGRAM_NAME) = @ARGV;
print "XML File: $XML_FILE ... \n";
print "XSLT File: $XSLT_FILE ... \n";
print "PRGM Name: $PROGRAM_NAME ... \n";
print "Output File: $OUTPUT_FILE ... \n";
#create output file
open(my $output_xml_file_name, '>', $OUTPUT_FILE);
my %in_params = (
PROGRAM => "$PROGRAM_NAME",
);
my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
print "Reading files ... \n\n";
my $source_xml = $parser->parse_file($XML_FILE);
my $xslt_doc = $parser->parse_file($XSLT_FILE);
my $stylesheet = $xslt->parse_stylesheet($xslt_doc);
print "Transforming XML ... \n\n";
my $results = $stylesheet->transform($source_xml, XML::LibXSLT::xpath_to_string(%in_params));
print "Saving XML ... \n\n";
print $output_xml_file_name "$results";
print "\nPerl End ... \n\n";
我遇到的问题是,对于500MB的超大型XML文件,出现以下错误。对于70-100MB的较小文件,它可以正常工作。如何将最大深度从250调整为5000?
输出错误:
runtime error: file 04_TestStylesheet.xsl element param
xsltApplyXSLTTemplate: A potential infinite template recursion was detected.
You can adjust xsltMaxDepth (--maxdepth) in order to raise the maximum number of nested template cal
ls and variables/params (currently set to 250).
根据以下用户的建议之一,我将此行更改为
my $xslt = XML::LibXSLT->new()->max_depth(1000);
但是现在我收到此错误。
Can't locate object method "parse_stylesheet" via package "250" (perhaps you forgot to load "250"?)
at perl_xslt_translator.pl line 38.
Here line38# my $stylesheet = $xslt->parse_stylesheet($xslt_doc);
答案 0 :(得分:1)
根据文档,只需将以下内容添加到您的程序中:
XML::LibXSLT->max_depth(1000);
请勿更改use XML::LibXSLT;
请勿更改my $xslt = XML::LibXSLT->new();