how to separate strings from descending xml nodes get by to_literal function from xml::xmllib module in perl?

StackOverflow https://stackoverflow.com/questions/9820279

  •  25-05-2021
  •  | 
  •  

Question

I have XML structure in like this:

<A>
  <B>One</B>
  <C>Two</C>
  <D>
     <E>Three</E>
     <F>Four</F>
     ...
  </D>
  ...
</A>

Structure is big, complex and I know only begining, i.e: '/A'

and code in perl like this:

use XML::LibXML;
my $parser = XML::LibXML->new();
my $xml = $parser->parse_file($file);
print $xml->find('/A')->to_literal;

gives me result:

OneTwoThreeFour

If I use code like:

print join (" ", map { $_->to_literal } $xml->findnodes('/A/descendant::*'));

I get:

One Two ThreeFour Three Four 

I need unique and sparate with withespaces values, i.e.:

One Two Three Four

How to do this?

Was it helpful?

Solution

The synopsis of XML::LibXML::Node gives you a hint:

@nodes = $node->findnodes( $xpath_expression );
$result = $node->find( $xpath );

So use the findnodes method to receive a list of results. Also, you want to select the children of the A element, change the XPath expression to fit:

my @children_literal_text = map { $_->to_literal } $xml->findnodes('/A/*');
# ('One', 'Two')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top