xpath question...

John J. Lee jjlee at reportlab.com
Sun Jul 9 11:00:15 EDT 2006


"bruce" <bedouglas at earthlink.net> writes:

> i have the following section of test code where i'm trying to get the
> attribute of a frame
>    <frame  src="....">
> 
> i'm trying to print/get the src value. the xpath query that i have displays
> the "src" attribute in the Xpather/Firefox plugin. however, i can't quite
> figure out how to get the underlying value in my test app...
> 
>   sxpath = "/html/frameset/frame[2]/attribute::src"
>     # s contains HTML not XML text
>   d = libxml2dom.parseString(s, html=1)
> 
>   #get the tr list
>   tr1 = d.xpath(sxpath)
> 
>   url = tr1[0]
> 
>   #get the url/link >>semester page
>   #link = br.find_link(nr=1)
> 
>   #url = link.url
>   print "link = ",url
>   sys.exit()
> 
> err output
> link =  <libxml2dom.Attribute object at 0xb7b7680c>
> 
> --------------------------------------
> 
> i'm not sure what i need to add to the line
>   url = tr1
> to resolve the issue/error...

It *looks* like "err output" is just a string you typed into your
message?  If so, that's not "an error" in the usual sense (there's no
traceback): rather, it's just output you didn't expect.

When somebody what you wrote, though, they must go through the
following laborious thought process: Is that string part of the
literal text output by your program, or are you indicating that you
saw a traceback that contains the following line ("link = ...")?  Or
is it something you just typed in to your message to indicate that the
result is unexpected to you?  If there's a traceback, post the full
traceback.  If that is the literal output, you should say so
explicitly, or make it clear through copy/paste of a shell session:

"""
$ my-test-prog.py
err output
link =  <libxml2dom.Attribute object at 0xb7b7680c>
$ 
"""

Back to your problem: The output is not unexpected, though (though I
don't know libxml2dom).  First, if you're bent on using XPath, you may
be better off with module lxml, which I think is a more recent and
friendlier wrapper of libxml2 / libxslt.  Second, you're almost there:
you're just getting back an object representing the attribute, rather
than the string you're looking for.  You simply need to ask the object
for its string representation.  How that's done depends on the module,
but it looks like you have to call a method explicitly in this case (I
can't find the libxml2dom docs easily).


John



More information about the Python-list mailing list