http://qs321.pair.com?node_id=1045351


in reply to Re^3: How can I force findnodes() to check value of attributes in either xpath1 or xpath2?
in thread How can I force findnodes() to check value of attributes in either xpath1 or xpath2?

The xml is as below:

<book:bookResource xmlns:book="http://api.iop.org/Book/1.0/"> <book:book> <book:meta doi="10.1088/bk111111" publisherId="bk111111" publisher="Pu +blishing" platform="science"> </book:meta> <book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/ +book/isbn/979-0- 4444-1000-17?releaseStatus=RELEASED" xlink:title="979-0-4444-1000-17" xlink:type="locator"> </book:locator> <book:contents> <book:chapter id="bk444444ch1" type="CHAPTER"> <book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/ +book/isbn/979-0- 4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED" xlink:role="http://www.iop.org/roles/book-part-locator" xlink:title="P +hotonic crystal light-emitting sources" xlink:type="locator"></book:locator> <book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/ +book/isbn/979-0- 4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED&amp; +format=pdf" xlink:role="http://www.iop.org/roles/book-part-pdf-locator" xlink:titl +e="Photonic crystal light-emitting sources" xlink:type="locator"></book:locator> <book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/ +book/isbn/979-0- 4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED&amp; +format=epub" xlink:role="http://www.iop.org/roles/book-part-epub-locator" xlink:tit +le="Photonic crystal light-emitting sources" xlink:type="locator"></book:locator> <book:meta doi="10.1088/bk444444ch1" firstPage="1-1" lastPage="1-118"> + <book:author givenName="J E" surname="Field"> <book:affiliation xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="bk444444ch1af1" xlink:role="http://www.iop.org/roles/affiliation-locator" xlink:type=" +locator"> </book:affiliation> </book:author> <book:affiliation name="Cavendish Laboratory, University of Cambridge" +> <book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="b +k444444ch1af1" xlink:role="http://www.iop.org/roles/affiliation-locator" xlink:type=" +locator"> </book:locator> </book:affiliation> </book:meta> </book:chapter> </book:contents> </book:book> </book:bookResource>

The complete code is :

my %book_columns = ( 'Book_Id' => '@publisherId', 'Book_Platform' => '@platform', 'Book_Publisher' => '@publisher', ); my %chapter_columns = ( 'Chapter_Doi' => 'book:meta/@doi', 'Chapter_Doi_Prefix' => 'substring-before(book +:meta/@doi,"/")', 'Chapter_Id' => '@id', 'Chapter_Title' => 'book:locator[contains(@xli +nk:href, "format=epub")]/@xlink:title', );
my $dom = $parser->parse_string(book.xml); my $root = $dom->documentElement(); my $xpc = XML::LibXML::XPathContext->new($root); $xpc->registerNs('book', 'http://api.iop.org/Book/1.0/'); $xpc->registerNs('xlink', 'http://www.w3.org/1999/xlink'); foreach my $chapter_node ($xpc->findnodes('/book:bookResource/book:boo +k/book:meta | /book:bookResource/book:book/book:contents/book:chapter +')) { foreach my $col(qw/Chapter_Id Chapter_Title Book_Id Book_Publisher + Book_Platform Chapter_Doi_Prefix Chapter_Doi Chapter_Id/) { print $xpc->findvalue($chapter_columns{$col}, $chapter_node); } }