Need to pull 2 nodes and combine the information i

2019-03-03 02:13发布

问题:

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.

The (very simplified) format of the xml is:

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

But occasionally it is like this:

<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:

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.

What I have been doing is this.

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);
 }

回答1:

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);
 }