Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

XML::XSLT problem

by bosshoff (Novice)
on Apr 24, 2005 at 14:28 UTC ( [id://450975]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,
This is my first post on Perl Monks. Basically, my problem is with the XML::XSLT perl module. I have the following code:
use strict; use XML::XSLT; print "Content-type: text/html\n\n"; open (XSL, "blog.xsl"); my $xslFile = ""; while(<XSL>) { $xslFile .= $_; } close(XSL); #my $xslFile = get("blog.xsl") || die qq(No feed "blog.xsl": $!); my $xslt = XML::XSLT->new ($xslFile); #, warnings => 1, debug => 1) or + die qq(Something Happened); my $xmlFile = ""; open (XML, "posts.xml"); while(<XML>) { $xmlFile .= $_; } close(XML); #print $xslt->serve $xmlFile; $xslt->transform ($xmlFile); print $xslt->toString; $xslt->dispose();
Which produces the following error when run:

no element found at line 1, column 0, byte -1 at /usr/lib/perl5/vendor_perl/5.8.6/i686-linux/XML/Parser.pm line 187

I have tried everything. The contents of the $xmlFile and $xslFile are all there (ie: there isn't a problem filling these vars with content). Processing these two files with Xalan takes less than a second and produces exactly what I want. The reason I can't use Xalan is because my web host only has XML::XSLT available. Thanks for the help.

Replies are listed 'Best First'.
Re: XML::XSLT problem
by rg0now (Chaplain) on Apr 24, 2005 at 16:13 UTC
    You do not want to read the contents of the XML file, just to apply the XSLT to it. Simply pass the name of the file to the transform method: (snippet taken from the man page of XML::XSLT):
    use XML::XSLT; my $xslt = XML::XSLT->new ($xsl, warnings => 1); $xslt->transform ($xmlfile); print $xslt->toString;
    This has the beneficial side-effect, that you will (hoepfully) get more descriptive error messages...
      I already tried that, and here is the new code:

      use strict; use XML::XSLT; myy $xslt = XML::XSLT->new ("blog.xsl", warnings => 1, debug => 1); $xslt->transform ("posts.xml"); print $xslt->toString; $xslt->dispose();
      Alas, same error. I was already making sure that the files did exist and that the variables were being filled, by outputing the vars; I know this isn't a simple file I/O error, in other words.

      What I find rather funny is that, when I use the deprecated document_transform function, it kindof works; it prints out only the first template match="/" stylesheet declaration and nothing else.

      Furthermore, I know it isn't my XSL or XML files because Xalan works flawlessly. There must be something I'm doing wrong, I just can't find out what, and that error message is not very descriptive (couldn't find anything at byte -1, really? lol)
        Ok, now the only thing remained to be done is to have a look at your actual blog.xsl and posts.xml files. A useful way to tackle these kinds of problems is if you try to find the minimal working and non-working examples, which then can be posted here. My guess is that either your XSL or XML is invalid, since the problem seems to rise right at the first line. You might not have the usual <xsl:stylesheet version="1.0" blablabla at the first line? Try to find some very basic XML and XSL files that do not work and try to find, what makes your specific version to fail.

        I myself have often found the solution for various problems just to try to create a basic test case, which can be posted to PerlMonks...

Re: XML::XSLT problem
by friedo (Prior) on Apr 24, 2005 at 15:51 UTC
    How do you know that $xmlFile and $xslFile are populated correctly? Neither of your opens are checked for failure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://450975]
Approved by moot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-19 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found