Why is XML::LibXML->toString() showing expected XML but findnodes() finding more XML?
我有这个子例程,该例程在xml块中传递,并试图找到一些元素(使用XML :: LibXML和XPath):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | sub new_from_xml { my $class = shift; my ( $xml ) = @_; my $self = {}; foreach (qw[ width height ]) { $self->{$_} = $xml->findnodes("//$_")->[0]->textContent; } $self->{type} = $xml->findnodes("//type")->[0]->textContent; $self->{url} = URI->new( $xml->findnodes("//url")->[0]->textContent ); return $class->new( $self ); } |
从这里打电话:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | sub new_from_xml { my $class = shift; my ( $xml ) = @_; my $self = {}; foreach (qw[id caption orientation]) { $self->{$_} = $xml->findnodes("//$_")->[0]->textContent; } $self->{alt} = $xml->findnodes('//htmlAlt')->[0]->textContent; foreach my $instance ( $xml->findnodes("//instance") ) { my $photo = WWW::NewsReach::Photo::Instance->new_from_xml( $instance ); push @{$self->{instances}}, $photo; } return $class->new( $self ); } |
我期望的是findnodes()返回的两个
这是我在调试器中看到的(我在上面的第一个子例程WWW :: NewsReach :: Photo :: Instance-> new_from_xml中)。
1 2 | DB<13> x $xml->toString 0 '<instance><width>100</width><height>66</height><type>Small</type><url>http://pictures.newsreach.co.uk/liveimages/Decor-tips-for-guaranteed-unsecured-loans-users.jpg</url></instance>' |
好吧,这就是我所期望的。
1 2 | DB<14> x $xml->findnodes("//type")->[0]->textContent 0 'Medium' |
等等,什么?这在toString显示的XML中不存在。这是哪里来的?
1 2 3 4 5 | DB<15> x $xml->findnodes("//type") 0 XML::LibXML::Element=SCALAR(0x101d03780) -> 4334788352 1 XML::LibXML::Element=SCALAR(0x101cdd5c0) -> 4334949168 |
嗯,所以有两个
1 2 | DB<16> x $xml->toString; 0 '<instance><width>100</width><height>66</height><type>Small</type><url>http://pictures.newsreach.co.uk/liveimages/Decor-tips-for-guaranteed-unsecured-loans-users.jpg</url></instance>' |
嗯,绝对只意味着有一个
为什么toString显示一个