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

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 ( #1043057=note: print w/ replies, xml ) Need Help??


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

Using http://www.xpathtester.com/ to fixup the xml I get

<?xml version="1.0" encoding="UTF-8"?> <book xmlns:book="http://generated.ns/book"> <book:chapter id="bk444444ch1" type="CHAPTER"> <book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:hre +f="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?releas +eStatus=RELEASED" xlink:title="Photonic crystal light-emitting source +s" xlink:type="locator"></book:locator> <book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:hre +f="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?releas +eStatus=RELEASED&amp;format=pdf" xlink:title="Photonic crystal light- +emitting sources" xlink:type="locator"></book:locator> <book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:hre +f="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?releas +eStatus=RELEASED&amp;format=epub" xlink:title="Photonic crystal light +-emitting sources" xlink:type="locator"></book:locator> </book:chapter> </book>

And then setns/query using xmllint --shell

$ xmllint.exe --shell ravi06-03.xml / > / > whereis //@xlink:href XPath error : Undefined namespace prefix xmlXPathEval: evaluation failed //@xlink:href: no such node / > / > setns xlink=http://www.w3.org/1999/xlink / > / > whereis //@xlink:href /book/book:chapter/book:locator[1]/@xlink:href /book/book:chapter/book:locator[2]/@xlink:href /book/book:chapter/book:locator[3]/@xlink:href / > / > cat //@xlink:href ------- xlink:href="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444c +h1?releaseStatus=RELEASED" ------- xlink:href="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444c +h1?releaseStatus=RELEASED&amp;format=pdf" ------- xlink:href="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444c +h1?releaseStatus=RELEASED&amp;format=epub" / > / > / > cat (//@xlink:href/..)[1] ------- <book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/ +book/isbn/979-0-4444-1000-17/book-part/chapter/bk4 44444ch1?releaseStatus=RELEASED" xlink:title="Photonic crystal light-e +mitting sources" xlink:type="locator"/> / > / > / > bye $


Comment on Re^2: How to parse xml with namespase vale in XMl:LibXML? (xmllint --shell setns / xpathtester)
Select or Download Code
Re^3: How to parse xml with namespase vale in XMl:LibXML? (xmllint --shell setns / xpathtester)
by ravi06 (Novice) on Jul 08, 2013 at 12:10 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?

      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Hi Choroba,

        Thanks a lot and sorry for being stupid. Actually, my objective was to get values of "xlink:href" and "xlink:title" for first occurrence of "book:locator" in Xpath context.

        Any hint or tweak on that?

      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
Node Status?
node history
Node Type: note [id://1043057]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2015-07-06 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (80 votes), past polls