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


in reply to Re: 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 Id, Title are in xpath1 an the other elements (Book Publisher Platform) are in xpath2. when the script is run, the out put it produce is "id","Title","","","" "","","Book","Publisher","Platform" whereas I want "id","Title","Book","Publisher","Platform"
  • Comment on Re^2: How can I force findnodes() to check value of attributes in either xpath1 or xpath2?

Replies are listed 'Best First'.
Re^3: How can I force findnodes() to check value of attributes in either xpath1 or xpath2?
by Corion (Patriarch) on Jul 19, 2013 at 11:42 UTC

    If you want to solve this problem using XPath, I recommend learning XPath.

    If you want to solve this problem using Perl, I recommend you show us the Perl code you have written to address this problem.

    The code you have shown so far does not even contain print statements, so it cannot relate to the problem you are having.

      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); } }

        Have you thought about not printing things if you don't want them printed?

        You could also try to make your program remember the current book while descending into the chapters.

        I would first find all book:bookResource nodes, and then do a second XPath search relative to each book:bookResource node. This will mean you have to understand how you can walk through an XML tree using XPath.