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


in reply to How can I force findnodes() to check value of attributes in either xpath1 or xpath2?

What Perl code have you written to address this problem?

Maybe you want to write a Perl program that first checks the attributes for xpath1 and then, if unsuccessful, checks the attributes for xpath2?

Or maybe you want to combine the two xpath queries into one?

I recommend modifying xpath1 so it returns all nodes with the appripriate Id attribute. If that is unsuccessful, run the (appropriately modified) xpath2. But that means you will have to learn XPath yourself.

  • Comment on Re: How can I force findnodes() to check value of attributes in either xpath1 or xpath2?

Replies are listed 'Best First'.
Re^2: How can I force findnodes() to check value of attributes in either xpath1 or xpath2?
by ravi06 (Novice) on Jul 19, 2013 at 11:31 UTC
    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"

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