需要拉2个节点并结合LibXML中的信息 [英] Need to pull 2 nodes and combine the information in LibXML

查看:10
本文介绍了需要拉2个节点并结合LibXML中的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我肯定需要一些帮助.首先,要温柔.我是 perl 和 LibXML 的新手.

I have an problem I could sure use some help with. First, be gentle. I am new to both perl and LibXML.

我一直在解析文档并将元素放入数组中,然后将其写入电子表格列.在测试过程中发现一些节点有多个同名子节点.我需要将每个子节点中的文本组合到数组的一个元素中.

I have been parsing a document and placing elements into an array that is then written to a speadsheet column. Durring testing it was discovered that some nodes have more than one child node of the same name. I need to combine the text from each of these child nodes into one element of the array.

xml 的(非常简化的)格式是:

The (very simplified) format of the xml is:

<Group>
    <title>
    <description>
    <reference>
    <fixtext>
    <check>
        <check-content> "Some text I want to pull"

但偶尔是这样的:

<Group>
    <title>
    <description>
    <reference>
    <fixtext>
    <check>
        <check-content> "Some text I want to pull"
        <check-content> "Some more text I want to pull and join to the first"

我可以从检查内容"中提取所有文本,但如果有多个文本,它会丢弃电子表格中的数据行.我需要能够说类似的话:

I can pull all the text from "check-contents", but if there is more than one it throws off the row of data in the spreadsheet. I need to be able to say something like:

如果有 2 个或更多检查内容"加入数据,则将数据推送到数组中.如果没有,只需将数据推送到数组中即可.

If there are 2 or more "check-content" join the data an push into the array. If not, just push the data into the array.

任何帮助将不胜感激.

我一直在做的是这个.

my @Check_Content;
my $Check_Content;
my $parser = XML::LibXML->new() or die $!;
my $doc1 = $parser->parse_file($filename1);
my $xc1 = XML::LibXML::XPathContext->new($doc1->documentElement() );
$xc1->registerNs(x => 'http://checklists.nist.gov/xccdf/1.1');

for my $Check_Content ($xc1->findnodes('//x:Group/x:Rule/x:check/x:check-content')) { 
     push (@Check_Content, $Check_Content->to_literal);
 }

推荐答案

你只需要处理每个 check 元素并为其生成 check-content 字符串:

You just need to process each check element and generate check-content string for it:

for my $Check ( $xc1->findnodes('//x:Group/x:Rule/x:check') ) { 

     my $result_string;

     for my $Check_Content ( $Check->findnodes('./x:check-content') ) { 
         $result_string .= $Check_Content->to_literal;
     }

     push (@Check_Content, $result_string);
 }

这篇关于需要拉2个节点并结合LibXML中的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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