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

查看:70
本文介绍了需要拉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.

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

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个或更多的"check-content",则将数据加入数组.如果没有,只需将数据推入数组即可.

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.

任何帮助将不胜感激.

Any help would be greatly appreciated.

我一直在做的是这个.

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天全站免登陆