Search code examples
perlxsltxslt-1.0exslt

XSLT 1.0 generation of a directory


Is there any facility available in XSLT 1.0 or exslt to generate a result document so that it also creates the directory(ies) in the path if they are not present? Or do I have to create the directory separately before generating the output document?

UPDATE

I am using Perl to perform the XSLT 1.0 transformation. Here is the code that I use.

#!/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 present\n";
}
else
{
    $isfile = "false";
    print "File is absent\n";
}

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;

And my XSL file is as follows

<?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>

I get following errors.

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

Even when I change the path as Dir1/Dir2/Dir3/outputfile1.xml (so that it uses the current directory where I have given full permissions),in the xsl file, I get following errors.

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

Does the libxslt in Perl 5.8.8 not support the directory creation?


Solution

  • Yes, one can write/read file and create directory in XSLT 1.0 if you are using Perl for triggering the transformation. As mentioned by Martin Honnen, all I had to do was add the few security callbacks such that it would all the XSLT engine to perform the file operations. Perl script now looks like

    #!/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 present\n";
    }
    else
    {
        $isfile = "false";
        print "File is absent\n";
    }
    
    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;