http://qs1969.pair.com?node_id=584600

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

Here is my code.
use XML::XSLT; use strict; use warnings; my $xsl = 'sample.xsl'; my $xmlfile = 'sample.xml'; my $xslt = XML::XSLT->new($xsl, warnings => 1);
It generates the message: Can't locate object method "new" via package "XML::XSLT" (perhaps you forgot to load "XML::XSLT"?) at program.pl line 6.

From docs, I get

XML::XSLT makes use of XML::DOM and LWP::Simple, while XML::DOM uses XML::Parser. Therefore XML::Parser, XML::DOM and LWP::Simple have to be installed properly for XML::XSLT to run.
By checking the modules with CPAN, I get following results:
XML::Parser is up to date (2.34).
XML::DOM is up to date (1.44).
LWP::Simple is up to date (1.41)

I might be missing something obvious, but cannot figure it out myself...

--Artist

Replies are listed 'Best First'.
Re: XML::XSLT problem
by bmann (Priest) on Nov 16, 2006 at 22:15 UTC
    Is XML::XSLT up to date? Old versions (0.20 and earlier) don't have a new method.

      I am using version 0.48 for XML::XSLT as recently downloaded from CPAN. I copied that in my home directory and now, I get the message : Can't call method "createDocumentFragment" on an undefined value at XML/XSLT.pm line 766.
      --Artist
        
        

        This sounds like you are trying to process a stylesheet without having loaded an XML document first. Please could you supply the smallest piece of code and sample XML and XSLT files that give this error - I'm pretty sure the code you have above wouldn't do this because it XML::DOM's createDocumentFragment() is only used when certain XSLT directives need to insert generated elements into the outout document.

        BTW don't bother following up the cpanforum posting, I'm not going to respond in both places.

        /J\

Re: XML::XSLT problem
by graff (Chancellor) on Nov 17, 2006 at 02:12 UTC
    It may be that there is an older version of XML::XSLT (lacking the "new" method) already installed somewhere in a "default" @INC path, and your perl script will always find that first.

    Try twiddling with @INC before loading the module -- e.g. put the path having the current version of the module in your PERL5LIB environment variable or on the shebang line: "-I/path/to/new/version".

    I haven't looked at the docs yet about how the various path settings are ordered at start-up, but I think one of those methods will do an "unshift" of the given path onto @INC (i.e. put it at the front of the list).

    As a last resort, you could do something like this in your script:

    # instead of use XML::XSLT; ... unshift @INC, "/path/to/new/version"; require XML::XSLT;
      I am trying to use WordXML file as my xml file and wordml2latex.xsl as Stylesheet but I get an 'createDocumentFragment' error.I tried the 2 solutions given in this forum but still unable to fix it.Would any one please help me. Thanks in advance.(my code is in below)
      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # instead of use XML::XSLT; ... #unshift @INC, "C:\\Perl64"; #require XML::XSLT; use XML::XSLT; #use XML::DOM; #use XML::Parser; #use XML::Simple; #use LWP::Simple; #WORDXML to XSLT(wordml2latex) using XML::XSLT my $xslfile_path = "C:\\temp\\wordml2latex.xsl"; my $xmlfile_path = "C:\\temp\\Sample document.xml"; open my $xmlfile, '<', $xmlfile_path or die "Cannot open '$xmlfile_pat +h': $!\n"; my $xmlfile_contents; { local $/ = undef; $xmlfile_contents = <$xmlfile>; close $xmlfile; } #print $xmlfile_contents; # exit 1; open my $xslfile, '<', $xslfile_path or die "Cannot open '$xslfile_pat +h': $!\n"; my $xslfile_contents; { local $/ = undef; $xslfile_contents = <$xslfile>; close $xslfile; } #print $xslfile_contents; my $xslt = XML::XSLT->new (Source => $xslfile_contents, warnings => 1) +; $xslt->transform ($xmlfile_contents); print $xslt->toString; $xslt->dispose();
        Why are you reading/slurping the file yourself instead of letting XML::XSLT read the file?