开发者

XML::LibXML: How to get a Number/Boolean-object with find?

From http://metacpan.org/pod/XML::LibXML::Node:

find evaluates the XPath 1.0 expression using the current node as the context of th开发者_运维知识库e expression, and returns the result depending on what type of result the XPath expression had. For example, the XPath "1 * 3 + 52" results in a XML::LibXML::Number object being returned. Other expressions might return a XML::LibXML::Boolean object, or a XML::LibXML::Literal object (a string).

I suppose in my example the find returns a XML::LibXML::Literal object (a string). Could someone show me examples where find returns a XML::LibXML::Number object resp. a XML::LibXML::Boolean object?

#!/usr/bin/env perl
use warnings; use strict;
use 5.012;
use XML::LibXML;

my $xml_string =<<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<filesystem>
   <path>
     <dirname>/var</dirname>
     <files>
       <action>delete</action>
       <age units="days">10</age>
     </files>
     <files>
       <action>delete</action>
       <age units="hours">96</age>
     </files>
   </path>
</filesystem>
EOF
#/

my $doc = XML::LibXML->load_xml( string => $xml_string );
my $root = $doc->documentElement;

say $root->find( '//files[1]/action' );

outputs

delete


$root -> find ("number(//files/age[@units = 'hours']"))


Your script prints delete because the objects returned by find overload the stringification operator "". The object returned is actually an XML::LibXML::NodeList. For example, the following

my $result = $root->find( '//files[1]/action' );
say $result;
say ref($result);

$result = $root->find( 'count(//files)' );
say $result;
say ref($result);

prints

delete
XML::LibXML::NodeList
2
XML::LibXML::Number
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜