j.goor has asked for the wisdom of the Perl Monks concerning the following question:

Consider this XSLT:
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" versi +on="1.0"> <xsl:output method="xml" indent="yes" encoding="utf-8"/> <xsl:param name='test'>testdata</xsl:param> <xsl:template match="/"> <TAG> <SOME_DATA> </SOME_DATA> </TAG> </xsl:template> </xsl:stylesheet>

Also, consider this perl snippet:
#perl; # INIT ---- use warnings; use strict; use Win32::OLE qw(in with); my $obj_xslt = Win32::OLE->new('MSXML2.DOMDocument.4.0') or die "new() + failed creating DOM"; $obj_xslt->{async} = "False"; $obj_xslt->load('<the_xslt_as_mentioned_above>'); # Get parameters from xslt my $xpath = "//xsl:param[\@name='test']"; my $q = $obj_xslt->selectSingleNode($xpath); print $q->item(0)->nodeValue, "\n";

My question: Why is this code not able to extract the value of the <xsl:param name='test'> variable? I tried about anything, no result however.
Can anyone help me retrieving the 'testdata' - data from the xslt?
I have to mention that this construct works fine with a 'plain xml' file. Since this xslt is also xml I reckon this should work also. Perhaps namespacing problem? I dunno. Please help!
Regards,
John

Replies are listed 'Best First'.
Re: Strange XML parser behaviour using MSXML2 Version 4.0 or not...?
by gellyfish (Monsignor) on Mar 16, 2005 at 10:19 UTC

    You do not have a Perl question, you have a question either about XPath or MSXML2 - you would be seeing the same if you had been writing this in VB or C++

    However this is nothting to do with XSLT but with the fact that you are using a QName (xsl:param in your expression) - the XPath implementation works only with unqualified localnames it appears, however if you were to use XML::LibXML (to bring it back to Perl again) this same expression would work.

    /J\

      If this problem is to be solved using XML::LibXML, it sounds like a parser-limitation, right? Using another parser is not an option unfortunately. And libXML will have its own problems as well, though it will surface another unfortunate moment.. ;-)
      To be honest I don't know exactly what you mean by 'Qualified name', so although this question might not be Perl-ish, I don't know where else to look. Given the fact that Mr. Gate's XML DOMDocument.4.0 API must be used, What would the (unqualified) XPath expression look like?
      Or is that an impossibility?
      Thanx in advance,
      John

        The behaviour is by design in MSXML2 and is clearly documented on MSDN here and here and here - if you have a problem with an MS API MSDN should be the first place that you will be looking.

        For more information on XML in general and the notion of "Qualified Name" in particular please refer to the W3 DOcumentation

        /J\

Re: Strange XML parser behaviour using MSXML2 Version 4.0 or not...?
by perlsen (Chaplain) on Mar 16, 2005 at 10:49 UTC

    Hi, I just tried your input xslt is an input file and tried using xml::Twig module
    i got the output. Is this your requirement?. if not i will give better result next time.

    use XML::Twig; my $twig = new XML::Twig( TwigRoots => { 'xsl:param[@name=\'test\']' = +> \&output }); $twig->parsefile( shift @ARGV ); sub output { my( $tree, $elem ) = @_; print $elem->text, "\n"; } OUTPUT: testdata

      If you have XML::Twig installed, note that you can use the xml_grep that comes with it:

      xml_grep -t 'xsl:param[@name="test"]' file.xslt

      The -t option is for text output (no tags around the result(s)), the rest should be pretty self-explanatory.

      Probably not of much help for the OP though...