Pull nodes from XML with perl and LibXML and parse the results

Viewed 714

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 spreadsheet column. During 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 everything below <check>, but cannot pull the <check-content> from that variable.

What I have been doing is:

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 ( $xc1->findnodes('//x:Group/x:Rule/x:check') ) { 

    print "This is Check $Check\n";

    my $result_string;

     for my $Check_Content ( $Check->findvalue('check-content') ) { 

     print "This is Check Content $Check_Content\n";

     $result_string .= $Check_Content->to_literal;
     }

 push (@Check_Content, $result_string);
 }

The first print statement proves that everything below <check> is being pulled. However the second print statement is always empty. It's as if it is no longer in XML format and it cannot pull the node.

I have used every variable of ('check-content') I could think of, ie

  • ('/check-content')
  • ('//check-content')
  • ('/x:check-content')
  • ('//check-content')
  • ('//x:check-content')

There could be one I missed, but I don't think so.

Any help would be greatly appreciated.

1 Answers
Related