Is this a valid XPath expression?

Posted by sid_com on Stack Overflow See other posts from Stack Overflow or by sid_com
Published on 2010-05-20T13:22:36Z Indexed on 2010/05/21 0:40 UTC
Read the original article Hit count: 449

Filed under:
|

Is this xpath a valid XPath expression? (It does what it should ).

#!/usr/bin/env perl
use strict; use warnings; use 5.012;
use XML::LibXML;

my $string =<<EOS;
<result>
    <cd>
    <artists>
        <artist class="1">Pumkinsingers</artist>
        <artist class="2">Max and Moritz</artist>
    </artists>
    <title>Hello, Hello</title>
    </cd>
    <cd>
    <artists>
        <artist class="3">Green Trees</artist>
        <artist class="4">The Leons</artist>
    </artists>
    <title>The Shield</title>
    </cd>
</result>
EOS
#/
my $parser = XML::LibXML->new();
my $doc = $parser->load_xml( string => $string );
my $root = $doc->documentElement;

my $xpath = '/result/cd[artists[artist[@class="2"]]]/title';

my @nodes = $root->findnodes( $xpath );
for my $node ( @nodes ) {
    say $node->textContent;
}

© Stack Overflow or respective owner

Related posts about xpath

Related posts about xml-libxml