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

Perlmonks In the sample xsl, xml and perl code below I'm trying to test fromDOMtoHash. When I run the code I get You must specify a XML::DOM::Document has the XML parameter at ./test.pl line 21 Although my unserstanding is that the $results arg was a DOM object when it was returned by transform. If I print $result I get XML::DOM::DocumentFragment=ARRAY(0x76b37c) Which seems to clearly indicate it is a DOM object.

XSL

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:test="urn:test"> <xsl:template match="/"> <xsl:variable name="matched" select="test:match_names('title | ti +tulo | titre | titolo', . )" /> <xsl:for-each select="$matched"> <xsl:copy-of select="." /> </xsl:for-each> </xsl:template> </xsl:stylesheet>

XML

<list> <title>System</title> <TituloGrande>Products</TituloGrande> <sublist> <SubTitleOne>Book</SubTitleOne> </sublist> </list>

CODE

#!/bin/perl # import required modules use XML::XSLT; use XML::Hash; use Data::Dumper; # define local variables my $xslfile = "test.xsl"; my $xmlfile = "test.xml"; # create an instance of XSL::XSLT processor my $xslt = XML::XSLT->new ($xslfile, warnings => 0, debug => 0); # transforms the XML file using the XSL style sheet my $results = $xslt->transform ($xmlfile); print $results; # Convertion from a XML::DOM::Document into a HASH my $xml_converter = XML::Hash->new(); my $xml_hash = $xml_converter->fromDOMtoHash($results); # send to output print Dumper($xml_hash); # free up some memory $xslt->dispose();

Replies are listed 'Best First'.
Re: XML::Hash fromDOMtoHash
by GotToBTru (Prior) on Nov 11, 2014 at 20:42 UTC

    Try this instead of the raw print:

    print $results->toString;

    I am no XSLT expert by any means, but I don't recognize what test:match_names is supposed to be doing. Also, you don't seem to account for the <list> layer of the xml.

    1 Peter 4:10
Re: XML::Hash fromDOMtoHash
by Jenda (Abbot) on Nov 14, 2014 at 01:56 UTC

    The module, unmaintained since 2008, asks for XML::DOM::Document, but you give it XML::DOM::DocumentFragment. Seems like it's a different thing.

    What if you forgot the nonsense named XSLT and the XML::Hash module and told us what is it you want to get?

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.