【问题标题】:Is there a reason to use the XML::LibXML::Number-object in my XML::LibXML-example?是否有理由在我的 XML::LibXML-example 中使用 XML::LibXML::Number-object?
【发布时间】:2010-05-19 05:25:18
【问题描述】:

在这个例子中,我得到时间'96'。是否存在我需要 XML::LibXML-Number-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="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

【问题讨论】:

  • 您能否阐明您要达到的目标,或许可以举一个较小的例子说明哪里出了问题?
  • 在这种情况下它不会出错 - 我正在搜索一个带有 XML::LibXML::Number-object 的示例,它可以说服我,需要 XML:: LibXML::Number-object。

标签: perl xml-libxml


【解决方案1】:

虽然我使用过很多 XML::LibXML,但我从未遇到过 XML::LibXML::Number 类。似乎存在允许 XPath 表达式对节点的文本内容进行数字断言(例如:> 10)。

如果您想要的只是数字“96”,那么最简单的方法可能是:

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

我发现对于获取多个值有用的习语是:

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

【讨论】:

    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    相关资源
    最近更新 更多