XSLT 1.0或exslt中是否有任何可用的工具来生成结果文档,以便在路径中不存在目录时也创建目录?还是必须在生成输出文档之前单独创建目录?
我使用Perl来执行XSLT 1.0转换。下面是我使用的代码:
#!/usr/local/bin/perl -w
use strict;
use warnings;
use File::Path;
use File::Spec;
use File::Basename;
use XML::LibXSLT;
use XML::LibXML;
my $isfile;
my ($xmlfile,$xsltfile,$samplefile) = qw/ Example.xml trans.xsl sample.xml/;
if(-f $samplefile)
{
$isfile = "true";
print "File is presentn";
}
else
{
$isfile = "false";
print "File is absentn";
}
my %args = ( "isfile" => $isfile );
my $xslt = XML::LibXSLT->new;
my $stylesheet = $xslt->parse_stylesheet_file($xsltfile);
my $results = $stylesheet->transform_file($xmlfile,XML::LibXSLT::xpath_to_string(%{args}));
0;
我的XSL文件如下
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xslt"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" media-type="text/xml"/>
<xsl:param name="isfile"/>
<xsl:template match="/">
<xsl:if test="$isfile = 'true'">
<exsl:document href = "/home/USER/Documents/XSLT/Dir1/Dir2/Dir3/outputfile1.xml" method="xml" version="1.0" encoding="UTF-8" indent="yes">
Article:- <xsl:value-of select="/Article/Title"/>
Authors:- <xsl:apply-templates select="/Article/Authors/Author"/>
</exsl:document>
</xsl:if>
</xsl:template>
<xsl:template match="Author">
<exsl:document href = "outputfile2.xml" method="xml" version="1.0" encoding="UTF-8" indent="yes">
always Generate this output!! <xsl:value-of select="." />
</exsl:document>
</xsl:template>
</xsl:stylesheet>
我得到以下错误:
runtime error: file trans.xsl line 24 element document
Directory creation for /home/USER/Documents/XSLT/Dir1/Dir2/Dir3/outputfile1.xml refused
runtime error: file trans.xsl line 24 element document
xsltDocumentElem: write rights for /home/USER/Documents/XSLT/Dir1/Dir2/Dir3/outputfile1.xml denied
at Transform.pl line 29
即使我将路径更改为Dir1/Dir2/Dir3/outputfile1.xml
(以便它使用我已授予完全权限的当前目录),在xsl文件中,我也会得到以下错误:
runtime error: file trans.xsl line 24 element document
Directory creation for Dir1/Dir2/Dir3/outputfile1.xml refused
runtime error: file trans.xsl line 24 element document
xsltDocumentElem: write rights for Dir1/Dir2/Dir3/outputfile1.xml denied
at Transform.pl line 29
Perl 5.8.8中的libxslt不支持创建目录吗?
我试过了
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
exclude-result-prefixes="xs exsl"
version="1.0">
<xsl:template match="/">
<exsl:document href="outputTest1/test2016082403Result.xml">
<foo>bar</foo>
</exsl:document>
</xsl:template>
</xsl:stylesheet>
在xsltproc/libxslt中创建了目录outputTest1
并包含了文件test2016082403Result.xml
,因此至少在xsltproc/libxslt中似乎支持创建目录。我不知道有任何其他支持exsl:document
的XSLT 1.0处理器。是的,如果使用Perl触发转换,可以在XSLT 1.0中写入/读取文件和创建目录。正如Martin Honnen所提到的,我所要做的就是添加几个安全回调,这样它就可以让XSLT引擎执行文件操作。Perl脚本现在看起来像
#!/usr/local/bin/perl -w
use strict;
use warnings;
use File::Path;
use File::Spec;
use File::Basename;
use XML::LibXSLT;
use XML::LibXML;
my $isfile;
my ($xmlfile,$xsltfile,$samplefile) = qw/ Example.xml trans.xsl sample.xml/;
if(-f $samplefile)
{
$isfile = "true";
print "File is presentn";
}
else
{
$isfile = "false";
print "File is absentn";
}
my %args = ( "isfile" => $isfile );
my $xslt = XML::LibXSLT->new;
my $stylesheet = $xslt->parse_stylesheet_file($xsltfile);
my $security = XML::LibXSLT::Security->new();
$security->register_callback( read_file => sub { return 1;} );
$security->register_callback( write_file => sub { return 1;} );
$security->register_callback( create_dir => sub { return 1;} );
$stylesheet->security_callbacks( $security );
my $results = $stylesheet->transform_file($xmlfile,XML::LibXSLT::xpath_to_string(%{args}));
0;