Skip Menu |

This queue is for tickets about the XML-LibXML CPAN distribution.

Report information
The Basics
Id: 87425
Status: resolved
Priority: 0/
Queue: XML-LibXML

People
Owner: Nobody in particular
Requestors: NGLENN [...] cpan.org
Cc: garfieldnate [...] gmail.com
AdminCc:

Bug Information
Severity: Important
Broken in: 2.0019
Fixed in: (no value)



CC: garfieldnate [...] gmail.com
Subject: unique_key method
Thank you for this module. I love it! In my application I need a hash where keys are nodes. Such a hash can be created (since objects can be stringified into keys), but it doesn't work right because nodes are only comparable using the isSameNode method. Querying the hash with a node object I get later may not find the entry. I would like a unique_key method (any name is fine) that would return a value usable in such a hash. I noticed that the implementation of isSameNode just does 'self == onode' (line 5009 of LibXML.xs), so I thought that identical nodes had the same address, and that the address should be usable as a unique ID. So I tried just doing 0+$node, but that didn't work right (a number comes back, nodes that would return true from isSameNode didn't have matching numbers). Without such a method, I will have to keep an extra index somewhere and call isSameNode over and over again.
RT-Send-CC: garfieldnate [...] gmail.com
I realized that dereferencing the node works (since it's a blessed integer). I noticed in the source (line 1252 of LibXML.pm) that numify (0+) is overloaded for nodes, but it returns the reference address. I think it could be really useful if instead it returned ${$self}. Then to get a unique ID for hashes or whatever you'd just have to numify the node. I don't know if current numification behavior has any specifically defined behavior, but using ${$self} would give $self->isSameNode($onode) iff 0+$self == 0+$self, which would be useful. use XML::LibXML; use Test::More tests => 3; my $parser = XML::LibXML->new(); my $html_doc = $parser->load_xml(string => '<xml><child></child></xml>'); my @first_children = $html_doc->documentElement->childNodes; my @second_children = $html_doc->documentElement->childNodes; ok($first_children[0] == $second_children[0], 'not sure what's being compared here but it passes'); ok(0+$first_children[0] != 0+$second_children[0], 'two separate objects with different addresses'); ok(${$first_children[0]} == ${$second_children[0]}, '... with the same underlying integer'); It may be that you'd rather use just do the dereference thing, but whatever the solution, documentation is needed. Show quoted text
> ID. So I tried just doing 0+$node, but that didn't work right
Hi NGLENN, On Tue Jul 30 01:40:47 2013, NGLENN wrote: Show quoted text
> I realized that dereferencing the node works (since it's a blessed > integer). I noticed in the source (line 1252 of LibXML.pm) that numify > (0+) is overloaded for nodes, but it returns the reference address. I > think it could be really useful if instead it returned ${$self}. Then > to get a unique ID for hashes or whatever you'd just have to numify > the node. I don't know if current numification behavior has any > specifically defined behavior, but using ${$self} would give $self-
> >isSameNode($onode) iff 0+$self == 0+$self, which would be useful.
> > use XML::LibXML; > use Test::More tests => 3; > my $parser = XML::LibXML->new(); > my $html_doc = $parser->load_xml(string => > '<xml><child></child></xml>'); > my @first_children = $html_doc->documentElement->childNodes; > my @second_children = $html_doc->documentElement->childNodes; > ok($first_children[0] == $second_children[0], 'not sure what's being > compared here but it passes'); > ok(0+$first_children[0] != 0+$second_children[0], 'two separate > objects with different addresses'); > ok(${$first_children[0]} == ${$second_children[0]}, '... with the same > underlying integer'); >
Well, first of all I suggest providing an API method for it instead of relying on ${$self} which is more subject to change and be broken. If you're interested, you can send me a pull request for that here: https://bitbucket.org/shlomif/perl-xml-libxml . Something else other people wanted was to be able attach some data on each node via the libxml2 mechanism, or perhaps even a hash of key/string-value pairs. See: http://code.activestate.com/lists/perl-xml/8953/ Will that help? Regards, -- Shlomi Fish Show quoted text
> It may be that you'd rather use just do the dereference thing, but > whatever the solution, documentation is needed. > >
> > ID. So I tried just doing 0+$node, but that didn't work right
RT-Send-CC: garfieldnate [...] gmail.com
Hi Shlomi, On Thu Aug 01 10:55:15 2013, SHLOMIF wrote: Show quoted text
> Hi NGLENN, > > On Tue Jul 30 01:40:47 2013, NGLENN wrote: > Well, first of all I suggest providing an API method for it instead of > relying on ${$self} which is more subject to change and be broken. If > you're interested, you can send me a pull request for that here: > https://bitbucket.org/shlomif/perl-xml-libxml .
I will do this! The isSameNode method just compares object addresses, so the key method will return the address as an integer (unless you think something else would be better). Show quoted text
> Something else other > people wanted was to be able attach some data on each node via the > libxml2 mechanism, or perhaps even a hash of key/string-value pairs. > See: > > http://code.activestate.com/lists/perl-xml/8953/ > > Will that help?
That would be a very useful feature if I could attach arbitrary scalars to nodes (I would put pointers to nodes or other objects). I could probably simplify my application with it. For now, though, I just need node hashes.
Resolving as fixed.
RT-Send-CC: garfieldnate [...] gmail.com
Okay. I plan on sending another pull request for namespaces (since they are sometimes returned from Node methods, like findnodes). I'm going to follow along the lines of the _IsEqual method and just return a string "prefix:href". It's not an integer like the other methods, but it is suitable for hash keys (and the other unique_key documentation doesn't say integer, so no one should be expecting it). On Sun Aug 18 05:20:39 2013, SHLOMIF wrote: Show quoted text
> Resolving as fixed.
Pull request sent. Sorry this took so long. I couldn't figure out why git clone kept failing. I forgot it should be hg clone! On Sun Aug 18 12:19:40 2013, NGLENN wrote: Show quoted text
> Okay. I plan on sending another pull request for namespaces