使用perl获取xml属性值。

时间:2022-11-27 07:58:43

I'm pretty new in Perl scripting. I need help to get the XML attribute value of total-alarms (in this case 11).

在Perl脚本中,我是新手。我需要帮助获取total-alarm的XML属性值(在本例中为11)。

<?xml version="1.0" encoding="utf-8"?>
<alarm-response-list xmlns="url.com" throttle="11" total-alarms="11">
  <alarm-responses>
    <alarm id="51ebf6fc-0571-1001-0162-008014000af00" />
    <alarm id="51ec0320-07af-1001-0162-008010400af00" />
    <alarm id="51ebf6f4-0564-1001-0162-008014000af00" />
    <alarm id="51ec1512-0bb2-1001-0162-008010400af00" />
    <alarm id="51ec157d-0bed-1001-0162-008010040af00" />
    <alarm id="51ebae1b-fa31-1000-0162-008010004af00" />
    <alarm id="51ec06d4-08bf-1001-0162-00801000a4f00" />
    <alarm id="51e8303f-740a-1000-0162-008014000af00" />
    <alarm id="51ebeabc-0352-1001-0162-008010400af00" />
    <alarm id="51ebf74d-0593-1001-0162-008010040af00" />
    <alarm id="51ebf78d-05b3-1001-0162-008010004af00" />
  </alarm-responses>
</alarm-response-list>

Unsuccessfully, I tried to use the following Perl code:

没有成功,我尝试使用以下Perl代码:

my $parser4 = XML::LibXML->new();
my $doc4 = $parser4->parse_file($current_working_dir.'\POSTres.xml');
my $xc = XML::LibXML::XPathContext->new( $doc4->documentElement()  );
my $nod = $xc->findnodes('//alarm-response-list');
print "A: ".$nod->getAttributes("total-alarms")."\n";

Any idea?

任何想法?

2 个解决方案

#1


4  

Here a way using XML::Twig module:

这里有一种使用XML::Twig模块的方法:

#!/usr/bin/env perl

use warnings;
use strict;
use XML::Twig;

my $twig = XML::Twig->new(
    twig_roots => {
        '/alarm-response-list' => sub { printf qq|%s\n|, $_->att('total-alarms') },
    },  
)->parsefile(shift);

Run it like:

运行该程序:

perl script.pl xmlfile

That yields:

收益率:

11

#2


3  

Your XPath expression doesn't match, because you specified a namespace in the XML document:

您的XPath表达式不匹配,因为您在XML文档中指定了一个名称空间:

<alarm-response-list xmlns="url.com" ...

If we register that namespace in the XPath context, then we can extract the data with XPath:

如果我们在XPath上下文中注册这个名称空间,那么我们可以使用XPath提取数据:

  1. Register the namespace. E.g.

    注册名称空间。如。

    $xc->registerNs('a' => "url.com");
    
  2. Use that namespace in the XPath, e.g.

    在XPath中使用该名称空间。

    my ($alarms) = $xc->findvalue('//a:alarm-response-list/@total-alarms');
    

This now extracts the value of the attribute, as expected.

这就像预期的那样,提取了属性的值。

#1


4  

Here a way using XML::Twig module:

这里有一种使用XML::Twig模块的方法:

#!/usr/bin/env perl

use warnings;
use strict;
use XML::Twig;

my $twig = XML::Twig->new(
    twig_roots => {
        '/alarm-response-list' => sub { printf qq|%s\n|, $_->att('total-alarms') },
    },  
)->parsefile(shift);

Run it like:

运行该程序:

perl script.pl xmlfile

That yields:

收益率:

11

#2


3  

Your XPath expression doesn't match, because you specified a namespace in the XML document:

您的XPath表达式不匹配,因为您在XML文档中指定了一个名称空间:

<alarm-response-list xmlns="url.com" ...

If we register that namespace in the XPath context, then we can extract the data with XPath:

如果我们在XPath上下文中注册这个名称空间,那么我们可以使用XPath提取数据:

  1. Register the namespace. E.g.

    注册名称空间。如。

    $xc->registerNs('a' => "url.com");
    
  2. Use that namespace in the XPath, e.g.

    在XPath中使用该名称空间。

    my ($alarms) = $xc->findvalue('//a:alarm-response-list/@total-alarms');
    

This now extracts the value of the attribute, as expected.

这就像预期的那样,提取了属性的值。