tej has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have an XSLT stylesheet which helps me to convert XML to HTML.

I want to execute this stylesheet through perl. When I run it on Stylus Editor It works fine without any error.

I tried using XML::XSLT and my code looks like:
use XML::XSLT; my $xsl="main.xsl"; my $xmlfile="main.xml"; my $xslt = XML::XSLT->new ($xsl, warnings => 1); $xslt->transform ($xmlfile); print $xslt->toString; $xslt->dispose();
and my XSLT file "main.xsl" looks like :
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="http://www.w3schools.com/furniture"> <xsl:output method="html" encoding="ANSI_X3.4-1986"/> <xsl:template match="chapter"> <html> <body> <div class="chap-title"><xsl:value-of select="f:title"/></div> <xsl:apply-templates select="f:sections"/> <!--<xsl:apply-templates select="f:bibliography"/>--> </body> </html> </xsl:template> <xsl:template match="f:section/f:section-title"> <div class="hd1"><br></br><xsl:value-of select="."/></div> </xsl:template> <xsl:template match="f:sections/f:section/f:para/text()"> <!--<xsl:apply-templates/>--> <div class="para1"><xsl:value-of select="."/></div> </xsl:template> <xsl:template match="f:section/f:section/f:section-title"> <div class="hd2"><br></br><xsl:value-of select="."/></div> </xsl:template> <xsl:variable name="level">0</xsl:variable> <xsl:template match="//f:list"> <xsl:variable name="val"> <xsl:number value="position()"/> </xsl:variable> <xsl:call-template name="recursive"> <xsl:with-param name="f:list"/> </xsl:call-template> </xsl:template> <xsl:template name="recursive" match="//f:list"> <xsl:param name="f:list" select="f:list-item"/> <xsl:for-each select="f:list-item"> <xsl:value-of select="/f:para"/><br></br> <div class="lt1"></div> <xsl:apply-templates/> </xsl:for-each> </xsl:template> <xsl:template match="f:section/f:section/f:section/f:section/f:section +-title"> <div class="hd3"><xsl:value-of select="."/></div> </xsl:template> <xsl:template match="f:sections/f:section/f:section/f:section/f:sectio +n/f:section-title"> <div class="hd3"><xsl:value-of select="."/></div> </xsl:template> <xsl:template match="f:sections/f:section/f:section/f:section/f:sectio +n-title"> <div class="hd4"><br></br><xsl:value-of select="."/></div> </xsl:template> </xsl:stylesheet>
When i run my pl from command prompt i get following error:
Can't call method "createDocumentFragment" on an undefined value at C +:/Perl/site /lib/XML/XSLT.pm line 766.

I dont understand what can be the problem..I tried searching a lot and probably it is in my xsl file. What can the possible error?

Also like We have toString method is there any method which I can use to create output file? Thanks

Replies are listed 'Best First'.
Re: XSLT to HTML/XHTML
by duyet (Friar) on Aug 05, 2011 at 08:32 UTC

    There seems to be an issue with the line:
    <xsl:variable name="level">0</xsl:variable>

    It would work when you change it to:
    <xsl:variable name="level" value="0" />

    See also XML::XSLT problem

      Thanks!! Now I dont get any error..

      But now i tried to print it to an output file like:
      use XML::XSLT; my $xsl="main.xsl"; my $xmlfile="main.xml"; my $xslt = XML::XSLT->new ($xsl, warnings => 1); $xslt->transform ($xmlfile); print $xslt->toString; ########### $main="main"; open (INF, ">$main.html")||warn ("Can not create file"); print INF $xslt; close INF; ############ $xslt->dispose();
      But when i open that HTML file i get output as XML::XSLT=HASH(0x248bdc)

      How can i print data to output file

      I tried installing XML::LibXSLT, however it is not installing on my system (version 5.12.2)

        $xslt is an object - you don't want to print that :) Instead try the below:

        $xslt->transform ($xmlfile); ########### $main="main.thml"; open (INF, ">$main")||warn ("Can not create file"); print INF $xslt->toString; close INF; ###########
Re: XSLT to HTML/XHTML
by Anonymous Monk on Aug 05, 2011 at 07:10 UTC

    I dont understand what can be the problem..I tried searching a lot and probably it is in my xsl file. What can the possible error?

    Probably a bug in XML::XSLT module

    You'll have better luck with XML::LibXSLT or maybe XML::Sablotron

      Probably a bug in XML::XSLT module

      I tried the same script on a sample file downloaded from w3schools and script works fine

      I 'll try these modules and check..

      Thanks.. :)
        Then you migth have a problem with features supported in the XSLT processor, for example, whether it's is version 1 or version 2 XSLT.