开发者

Is there a reason to use the XML::LibXML::Number-object in my XML::LibXML-example?

In this example I get to开发者_Go百科 times '96'. Is there a possible case where I would need a XML::LibXML-Number-object to to achieve the goal?

#!/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="hours">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;

my $result = $root->find( '//files/age[@units="hours"]' );
$result = $result->get_node( 1 );
say ref $result; # XML::LibXML::Element
say $result->textContent; # 96

$result =  $root->find ( 'number( //files/age[@units="hours"] )' );
say ref $result; # XML::LibXML::Number
say $result; # 96


Although I've used XML::LibXML quite a bit I have never encountered the XML::LibXML::Number class. It seems to exist to allow XPath expressions to make numerical assertions about the text content of a node (e.g.: > 10).

If all you want is the number '96' then the easiest way is probably:

my $result = $root->findvalue( '//files/age[@units="hours"]' );

An idiom I find useful for getting multiple values is:

my @values = map { $_->to_literal } $doc->find('//files/age');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜