Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How to parse xml with namespase vale in XMl:LibXML? ( XPath error : Undefined namespace prefix )

by Anonymous Monk
on Jul 08, 2013 at 09:21 UTC ( [id://1043055]=note: print w/replies, xml ) Need Help??


in reply to How to parse xml with namespase value in XMl:LibXML?

Its simple, define the namespace in an xpathcontext -- isn't xml/xpath fun :)

It also helps to have valid xml

Re: XML::LibXML and namespaces, Re^2: Namespaced XML::LibXML XPath query (not a bug)

The XML FIles: Understanding XML Namespaces, Re: XML::LibXSLT ignoring namespace on some Win32 machines (libxslt-1.1.27 bug), Re^2: XML::LibXML and namespaces, Re^2: XML::LibXML and namespaces, Re: XML::LibXML and namespaces, XPATH DOM traverse html/xml,

  • Comment on Re: How to parse xml with namespase vale in XMl:LibXML? ( XPath error : Undefined namespace prefix )

Replies are listed 'Best First'.
Re^2: How to parse xml with namespase vale in XMl:LibXML? (xmllint --shell setns / xpathtester)
by Anonymous Monk on Jul 08, 2013 at 09:40 UTC
      But when I am trying to access the nodes as below I don't get any value.
      print $chapter_node->findvalue('./book:locator/@xlink:href') . "\n";
      Could you please elaborate a bit or point out where I am committing mistake?
        Works for me:
        #!/usr/bin/perl use warnings; use strict; use XML::LibXML; my $xml = XML::LibXML->load_xml(IO => *DATA); my $root = $xml->documentElement; my $chapter_node = $root->findnodes('book:chapter')->[0]; print $chapter_node->findvalue('./book:locator/@*') . "\n"; __DATA__ <book xmlns:book='b'> <!-- Namespace definition missing in the origina +l data --> <book:chapter id="bk444444ch1" type="CHAPTER"> <book:locator xlink:href="/book/isbn/979-0-4444-1000-17/book-part/chap +ter/bk444444ch1?releaseStatus=RELEASED" xlink:title="Photonic crystal + light-emitting sources" xlink:type="locator" xmlns:xlink="http://www +.w3.org/1999/xlink"/> <book:locator xlink:href="/book/isbn/979-0-4444-1000-17/book-part/chap +ter/bk444444ch1?releaseStatus=RELEASED&amp;format=pdf" xlink:title="P +hotonic crystal light-emitting sources" xlink:type="locator" xmlns:xl +ink="http://www.w3.org/1999/xlink"/> <book:locator xlink:href="/book/isbn/979-0-4444-1000-17/book-part/chap +ter/bk444444ch1?releaseStatus=RELEASED&amp;format=epub" xlink:title=" +Photonic crystal light-emitting sources" xlink:type="locator" xmlns:x +link="http://www.w3.org/1999/xlink"/> </book:chapter> <!-- Missing in the original data --> </book>

        Note the changes I made to your data. Maybe your $chapter_node contains something else?

        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Could you please elaborate a bit or point out where I am committing mistake?

        Well, no, you don't show self-contained code . I guess that you didn't read my nodes and setup xpc with some setns, but I don't really have any way of knowing; also, I hate xml

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-19 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found