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

Hello, I was just wondering if someone could offer up some code to show how to properly use the XML::XSLT module; I have been trying to do this for some time, and have come up with nothing. What I would like is someone to use a simple xml and xsl file, and have the perl code make a base URL, even if it never gets used. The source file for the XML::XSLT module provides some good examples if you didn't want to write your own XML XSLT.

I keep getting errors like the following, completely undescriptive (of course ;):
Error while parsing: 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

Replies are listed 'Best First'.
Re: XML::XSLT 'base' parameter
by cees (Curate) on May 01, 2005 at 12:53 UTC

    I haven't used that module before, but it sounds to me like you are not loading the XML or XSL file properly. It is complaining about not finding the right tag at the first character of the first line, which sounds like there might be nothing there at all.

    If it isn't that, then maybe your XML isn't well formed. It would help to show some example code, so that anyone trying to help doesn't have to start from scratch to try to answer your question.

    - Cees

      It won't even parse with the sample code when the base parameter is set. Consult XML::XSLT dir/samples/* for examples so you don't have to write your own.
Re: XML::XSLT 'base' parameter
by gellyfish (Monsignor) on May 05, 2005 at 09:12 UTC

    Please can you supply the shortest possible piece of code that exhibits this behaviour using the base argument along with some sample XML and XSLT. It is not clear what you are trying to do.

    /J\

      blog.pl:
      use XML::XSLT; use LWP::Simple; use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; my $xsl = get("blog2.xsl") my $xslt = XML::XSLT->new ($xsl, warnings => 1, base => "/home/bosshof +f/www/blog/"); my $xml = get("posts.xml") $xslt->transform ($xml); print $xslt->toString; $xslt->dispose();
      blog2.xsl:
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr +ansform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="xml" indent="yes" encoding="UTF-8"/> <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/> <xsl:output doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-tra +nsitional.dtd"/> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test</title> <style type="text/css"> @import url("main.css"); </style> </head> <body> <xsl:call-template name="topMenu"/> </body> </html> </xsl:template> <xsl:template name="topMenu"> <div id="topMenu"> <xsl:for-each select="document('menu.xml')/menu/item"> <span class="topMenu"> <xsl:element name="a"> <xsl:attribute name="href"><xsl:value-of select="href"/></ +xsl:attribute> <xsl:value-of select="name"/> </xsl:element> </span> </xsl:for-each> </div> </xsl:template> </xsl:stylesheet>
      posts.xml:
      <?xml version="1.0" encoding="UTF-8"?> <posts> <post title="Setting Up Gentoo" date="2005-2-24" id="1"> <para firstLetter="T">he kernel I'll be going with is 2.6.10-gentoo-r6 +, which is the most recent gentoo-enhanced version of the kernel to d +ate. The stage I've chosen to start from is a Stage 2 install: basic +ally, all the low-level system stuff is already provided, which I wou +ld need anyway. No, I'm not yet nuts enough to do a Stage 1. If you + are confused about what I'm talking about, please go to <url>http:// +www.gentoo.org/doc/en/handbook/handbook-x86.xml</url>. That is the g +uide I used to install my system.</para> <para>Though this server will mostly be used for server operations, I +would like a window manager to make rooting through configurations ea +sier. I chose fluxbox (<url>http://fluxbox.sourceforge.net/</url>) f +or its very small overhead, unlike KDE or Gnome, which insist on inst +alling a bunch of stuff I won't need (including all that lame GUI con +fig crap).</para> </post> </posts>
      menu.xml:
      <?xml version="1.0" encoding="UTF-8"?> <menu> <item> <name>Home</name> <href>http://pansy/main.xhtml</href> </item> <item> <name>Somewhere else</name> <href>#</href> </item> </menu>
      It won't work unless you comment out the document command and take out the base argument when instantiating the XML::XSLT object.

        The 'base' arguement expects a base URL. You have given it a base directory. Try using 'file:///home/bosshoff/www/blog/' as the base arguement instead.

        - Cees