in reply to Comparing nodes in LibXML

After lunch I hacked this up that solves my problem. But there must be a better way...

sub _cmpXML( $$ ){ # Pass in two XML nodes and return 0 if they are the same, else # lexographically compare the namespaces fist then the node names # (with out prefexes) my $n1 = shift or die; my $n2 = shift or die; my $ns1 = $n1->namespaceURI(); my $ns2 = $n2->namespaceURI(); $ns1 ne $ns2 and return $ns1 cmp $ns2; my $nn1 = $n1->nodeName(); my $nn2 = $n2->nodeName(); $nn1 =~ s/^[^\:]*:?//; $nn2 =~ s/^[^\:]*:?//; return $nn1 cmp $nn2; }

There must be a built in for doing this, it is a bit inconceivable to me that this is not a common requirement

Replies are listed 'Best First'.
Re^2: Comparing nodes in LibXML
by tangent (Parson) on Sep 03, 2015 at 02:05 UTC
    my $nn1 = $c1->localname(); my $nn2 = $c2->localname(); print "\$nn1 $nn1\n\$nn2 $nn2\n\n"; Output: $nn1 Authors $nn2 Authors

      localname is helpful. I missed it in the documentation. But it is only half the picture as it strips namespace information. Evven though it seems that namespaces are an abomination in XML I need to account for them. I am not generating the XML I have to eat.

        You can get the prefix at the same time
        my ($prefix,$name) = ( $node->prefix(), $node->localname() );