Subject: | contains() Works Only for First Element |
Date: | Sat, 18 Nov 2017 15:45:44 +0000 |
To: | "bug-xml-libxml [...] rt.cpan.org" <bug-xml-libxml [...] rt.cpan.org> |
From: | Tom Moreau <tom.moreau [...] live.ca> |
Given the following XML doc:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<author>James McGovern</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>Per Bothner</author>
<author>James McGovern</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>
If I use the contains() function for books that have an author whose name contains the string McGovern, it works only if McGovern is the first author. Test script below:
use strict 'vars';
use IO::Handle;
use XML::Simple;
use XML::LibXML qw( );
use Data::Dumper;
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file('Books.xml');
my @Titles = map {$_->to_literal} $doc->findnodes(q {//book[contains(author, "McGovern")]/title});
# my @Titles = map {$_->to_literal} $doc->findnodes(q {//book[author = "James McGovern"]/title});
print Dumper (@Titles);
If I specify an exact match on the full author name, it finds the books.
Tom
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
Cambridge, ON Canada tom.moreau@live.ca<mailto:tom.moreau@live.ca>