-1

I am using this this answer to perform XSLT 1.0 tranformation. I intend save the output in another xml file. As such I add,

use strict;
use warnings;

use XML::LibXSLT;

my ($xmlfile, $xsltfile,$outfile) = qw/ example.xml trans.xsl out.xml /;

my $xslt = XML::LibXSLT->new;
my $stylesheet = $xslt->parse_stylesheet_file($xsltfile);
my $results    = $stylesheet->transform_file($xmlfile);

$stylesheet->output_file($results,$outfile);

This produces following error,

Can't coerce UNKNOWN to string in entersub at $LongPath/XML/LibXSLT.pm line 485.

Looking up online, I was able to find this blog which mentions something similar.

Am I missing something obvious?

UPDATE

XML File

<?xml version="1.0"?>
<?xml-stylesheet type="xsl" href="trans.xsl"?>
<Article>
  <Title>My Article</Title>
  <Authors>
    <Author>Mr. Foo</Author>
    <Author>Mr. Bar</Author>
  </Authors>
  <Body>This is my article text.</Body>
</Article>

XSL File

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    
  <xsl:output method="xml"/>    
  <xsl:template match="/">
    Article - <xsl:value-of select="/Article/Title"/>
    Authors: <xsl:apply-templates select="/Article/Authors/Author"/>
  </xsl:template>    
  <xsl:template match="Author">
    - <xsl:value-of select="." />
  </xsl:template>    
</xsl:stylesheet>
Community
  • 1
  • 1
Recker
  • 1,915
  • 25
  • 55

1 Answers1

0

I ended up finding the problem myself. It appears that

$stylesheet->output_file($results,$outfile);

statement was not able to create the file with the path supplied. This path had few non existent directories. As such I ended up doing

my $dir = dirname($outfile);
mkpath($dir);

after which I was able to save the output to $outfile.

Recker
  • 1,915
  • 25
  • 55