The basic problem is your XPath expression doesn't follow the structure of the document.
<mt> is not a child of
<md>, there's a
<mi> in between. Therefore, something like the following should match all the VAL nodes:
/mdc/md/mi/mt[contains(.,"VAL")]
The same holds for the <r> elements: their parent is <mv>.
The following works for me:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use XML::LibXML;
my @val;
my $doc = 'XML::LibXML'->load_xml( location => 'file.xml' );
for my $book ($doc->findnodes('/mdc/md/mi/mt[contains(.,"VAL")]')) {
my $order = 1 + $book->findvalue('count(preceding-sibling::mt)');
my $rs = $book->findnodes("../mv/r[$order]");
say join ', ', map $_->textContent, $book, @$rs;
}
You can get the same logic with XML::XSH2, which is a wrapper around XML::LibXML:
open file.xml ;
for /mdc/md/mi/mt[xsh:match(.,'^VAL[0-9]+')] {
my $order = 1 + count(preceding-sibling::mt) ;
echo xsh:join(', ', (.), ../mv/r[$order]) ;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.