Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Not able to register namespace in XML::LibXML on Linux.

by ravi06 (Novice)
on Jul 15, 2013 at 09:47 UTC ( #1044337=perlquestion: print w/replies, xml ) Need Help??

ravi06 has asked for the wisdom of the Perl Monks concerning the following question:

What is wrong with registerNs()?
my $dom = $parser->parse_file("book.xml"); my $root = $dom->documentElement(); my $xpc = XML::LibXML::XPathContext->new($root); $xpc->registerNs(xlink => "") or die "Coul +dn't register Namespace - $!";
The xml file is :
<?xml version="1.0" encoding="UTF-8"?> <book:bookResource xmlns:xlink=""> <book:book> <book:chapter id="bk111111ch1" type="CHAPTER"> <book:locator xlink:href="/book/isbn/979-0-1234-1111-1/book-part/chapt +er/bk111111ch1?releaseStatus=UNRELEASED" xlink:title="Photonic crysta +l light-emitting sources" xlink:type="locator" xmlns:xlink="http://ww"/> <book:locator xlink:href="/book/isbn/979-0-1234-1111-1/book-part/chapt +er/bk111111ch1?releaseStatus=UNRELEASED&amp;format=pdf" xlink:title=" +Photonic crystal light-emitting sources" xlink:type="locator" xmlns:x +link=""/> <book:locator xlink:href="/book/isbn/979-0-1234-1111-1/book-part/chapt +er/bk111111ch1?releaseStatus=UNRELEASED&amp;format=epub" xlink:title= +"Photonic crystal light-emitting sources" xlink:type="locator" xmlns: +xlink=""/> <book:meta doi="10.1088/bk111111ch1" firstPage="1-1" lastPage="1-118"> <book:author givenName="Roger" surname="Ebert"><book:affiliation xlink +:href="bk111111ch1af1" xlink:type="locator" xmlns:xlink="http://www.w"/></book:author> <book:affiliation name="Cavendish Laboratory, University of Cambridge" +><book:locator xlink:href="bk111111ch1af1" xlink:type="locator" xmlns +:xlink=""/></book:affiliation> </book:meta> </book:chapter> </book:book> </book:bookResource>

Replies are listed 'Best First'.
Re: Not able to register namespace in XML::LibXML on Linux. (or die)
by Anonymous Monk on Jul 15, 2013 at 10:04 UTC
    Why did you add or die? How is registerNs documented to signal failure? Does registerNs use $!?
      Why registerNs when you don't have to?
      #!/usr/bin/perl -- use strict; use warnings; use XML::LibXML; use Data::Dump qw/ dd pp /; Main( @ARGV ); exit( 0 ); sub Main { my $dom = XML::LibXML->new( qw/ recover 2 / )->load_xml( location => shift || q{ravi06-05.xml}, ); print $_->nodePath,"\n" for $dom->findnodes(q{//@xlink:href}); print $_->nodePath,"\n" for $dom->findnodes(q{//book:locator/@xlin +k:href}); } __END__ /book:bookResource/book:book/book:chapter/book:locator[1]/@xlink:href /book:bookResource/book:book/book:chapter/book:locator[2]/@xlink:href /book:bookResource/book:book/book:chapter/book:locator[3]/@xlink:href /book:bookResource/book:book/book:chapter/book:meta/book:author/book:a +ffiliation/@xlink:href /book:bookResource/book:book/book:chapter/book:meta/book:affiliation/b +ook:locator/@xlink:href /book:bookResource/book:book/book:chapter/book:locator[1]/@xlink:href /book:bookResource/book:book/book:chapter/book:locator[2]/@xlink:href /book:bookResource/book:book/book:chapter/book:locator[3]/@xlink:href /book:bookResource/book:book/book:chapter/book:meta/book:affiliation/b +ook:locator/@xlink:href
        Hi, we don't have Data::Dump. We can't use predicates("book:locator1") either.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1044337]
Approved by NetWallah
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-07-03 23:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found