I need to compare XML that may come from different sources where name space prefixes will be defined separately and perhaps differently.

#!/usr/bin/perl -w use XML::LibXML; my $parser = XML::LibXML->new(); my $xmls1 = '<?xml version="1.0" encoding="utf-8" ?> '. '<LIST '. 'xmlns:Z1="TheNameSpace"> '. '<Z1:Authors/>'. '</LIST>'; my $xmls2 = '<?xml version="1.0" encoding="utf-8" ?> '. '<LIST '. 'xmlns:Z2="TheNameSpace"> '. '<Z2:Authors/>'. '</LIST>'; my $xml1 = $parser->parse_string($xmls1)->documentElement(); my $xml2 = $parser->parse_string($xmls2)->documentElement(); my @c1 = $xml1->nonBlankChildNodes(); my $c1 = $c1[0]; my @c2 = $xml2->nonBlankChildNodes(); my $c2 = $c2[0]; my $c1ref = ref($c1); my $c2ref = ref($c2); print "\$c1ref $c1ref \$c2ref $c2ref\n"; print $c1->toString()."\n"; print $c2->toString()."\n"; my $ns1 = $c1->namespaceURI(); my $ns2 = $c2->namespaceURI(); my $nn1 = $c1->nodeName(); my $nn2 = $c2->nodeName(); print "\$ns1 $ns1\n\$ns2 $ns2\n\n"; print "\$nn1 $nn1\n\$nn2 $nn2\n\n"; print $c1->isEqual($c2)?"Equal\n":"Not\n";

$c1 and $c2 should be the same. I could compare the namespaces from namespaceURI() and strip off the prefix from nodeName(), that would work, but there must be a better way. Is there?

BTW the output I get from that code is:

$c1ref XML::LibXML::Element $c2ref XML::LibXML::Element <Z1:Authors/> <Z2:Authors/> $ns1 TheNameSpace $ns2 TheNameSpace $nn1 Z1:Authors $nn2 Z2:Authors Not

In reply to Comparing nodes in LibXML by worik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.