在Windows 7上,我尝试使用Perl v5.8.8和libXSLT 1.66版转换单个文档。这是我的用例的低调版本。
Perl脚本
use strict;
use warnings;
use File::Path;
use File::Spec;
use File::Basename;
use XML::LibXSLT;
use XML::LibXML;
use Getopt::Std;
my $isfile;
my ($xmlfile,$xsltfile,$samplefile) = qw/ Example.xml trans.xsl sample.xml/;
my %opts = ();
getopts('o:',\%opts);
my $outPath = $opts{'o'};
die "Resource Location/Directory not specified" if !defined($outPath);
if(-f $samplefile)
{
$isfile = "true";
print "File is present\n";
}
else
{
$isfile = "false";
print "File is absent\n";
}
my %args = ( "isfile" => $isfile,"outpath" => $outPath, );
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;} );
$stylesheet->security_callbacks( $security );
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"
xmlns:date="http://exslt.org/dates-and-times"
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:param name="outpath"/>
<xsl:variable name="current-year">
<xsl:value-of select="date:year()"/>
</xsl:variable>
<xsl:template match="/">
<xsl:if test="$isfile = 'true'">
<exsl:document href = "{$outpath}" method="xml" version="1.0" encoding="UTF-8" indent="yes">
Article:- <xsl:value-of select="exsl:node-set(/Article/Title)|exsl:node-set(/Article/Title)/@context"/>
Authors:- <xsl:apply-templates select="/Article/Authors/Author"/>
</exsl:document>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
要转换的文件
<?xml version="1.0"?>
<?xml-stylesheet type="xsl" href="trans.xsl"?>
<Article>
<Title context="This article">My Article</Title>
<Authors>
<Author>Mr. Foo</Author>
<Author>Mr. Bar</Author>
</Authors>
<Body>This is my article text.</Body>
</Article>
我事先创建了需要生成文档的目录。然后我运行我的脚本如下
perl Transform.pl -o C:\user\Documents\XSLT\dir1\dir2\output1.xml
未在预期路径中创建文件。相反,当我在DIR
目录(我保留转换脚本,xsl文件和输入xml文件)中ls
或XSLT
时,我会得到关注,
%5Cuser%5CDocuments%5CXSLT%5Cdir1%5Cdir2%5Coutput1.xml dir1/ Example.xml sample.xml Transform.pl trans.xsl
但是,当我在选项-o
的命令行参数上提供路径时,如下所示,
C:\user\Documents\XSLT\dir1\dir2\output1.xml
文件在适当的提供路径中创建。
1)当我提供Windows特定路径时,为什么exsl-document
表现得很奇怪?
2)另外,为什么在路径中添加%5C
(相当于/
的ASCII)?
3)是否不理解如何将路径分隔符的Windows特定路径解释为/
?