从使用名称空间的XML文档中提取数据 [英] Extracting data from an XML document that uses namespaces

查看:124
本文介绍了从使用名称空间的XML文档中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些XML文件,我想在其中使用一些信息.我写了一个代码,先读取这些文件,然后查找一些条件.

I have some XML files where I want to use some information from them. I have written a code that reads those files and then looks for some conditions.

问题在于这些XML文件以

The problem is that these XML file begins with

   <SquishReport version="2.1" xmlns="http://www.froglogic.com/XML2">

,Perl无法读取它们(至少在我的代码中!).但是当我将这些行添加到XML文件的第一行

and Perl could not read them (at least in my code!). But When I am appending these lines in the first line of XML file

   <?xml version="1.0" encoding="UTF-8"?>
   <?xml-stylesheet type="text/xsl"?>

效果很好.

我的XML文件test.xml中的某些行:

Some lines from my XML file test.xml:

<SquishReport version="2.1" xmlns="http://www.froglogic.com/XML2">
   <test name="TEST">
      <prolog time="2015-10-01T03:45:22+02:00"/>
      <test name="tst_start_app">
          <prolog time="2015-02-01T03:45:23+02:00"/>
          <message line="38" type="LOG" file="C:\squish\test\sources.py" time="2015-02-01T03:45:23+02:00">
              <description>
                <![CDATA[>>  >>  >> start: init (global) - testcase C:\squish\test\tst_start_app]]></description>
          </message>
       </test>
   </test>
</SquishReport>

,用于读取XML文件的Perl代码为:

and the Perl code for reading the XML file is:

use strict;
use warnings;
use feature 'say';
use XML::LibXML;

# Parse the XML
my $xml = XML::LibXML->load_xml(location => 'test.xml');

# Iterate the entries
for my $entry ($xml->findnodes('/SquishReport/test/test')) {
    my $key = $entry->findvalue('@name');
    say "$key";
}

推荐答案

该文档的根节点是在http://www.froglogic.com/XML2名称空间中名称为SquishReport的元素.简而言之,我们可以说根节点是

The root node of that document is an element which has name SquishReport in the http://www.froglogic.com/XML2 namespace. Concisely, we can say the root node is a

{http://www.froglogic.com/XML2}SquishReport


当在XPath中使用SquishReport(而不是prefix:SquishReport)时,它将尝试匹配null名称空间中名称为SquishReport的元素.简而言之,我们可以说它尝试匹配一个


When one uses SquishReport (as opposed to prefix:SquishReport) in an XPath, that tries to match an element which has name SquishReport in the null namespace. Concisely, we can say it attempts to match a

{}SquishReport


要指定名称空间,可以使用 context ,如下所示:

use strict;
use warnings;
use feature qw( say );

use XML::LibXML               qw( );
use XML::LibXML::XPathContext qw( );

my $xpc = XML::LibXML::XPathContext->new();
$xpc->registerNs(sr => 'http://www.froglogic.com/XML2');

my $doc = XML::LibXML->load_xml( location => 'test.xml' );
for my $entry ($xpc->findnodes('/sr:SquishReport/sr:test/sr:test', $doc)) {
    my $key = $entry->findvalue('@name');
    say $key;
}


注意:XPath中使用的前缀与XML文档中使用的前缀(如果有)没有关系.您应该知道要搜索的元素所在的名称空间,但不知道给定文档使用的前缀.


Note: The prefix used in the XPath have no relation to the prefixes used in the XML document (if any). You are expected to know the namespace in which resides the elements for which you are searching, but not the prefixes used by a given document.

这篇关于从使用名称空间的XML文档中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆