Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by ravi06 (Novice)
on Jul 19, 2013 at 12:44 UTC ( #1045351=note: print w/replies, xml ) Need Help??


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); } }
  • Comment on Re^4: How can I force findnodes() to check value of attributes in either xpath1 or xpath2?
  • Select or Download Code

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

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1045351]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2022-01-19 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (55 votes). Check out past polls.

    Notices?