470,613 Members | 1,645 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

Using xml.xpath question.

0wl
Hi,

I am trying to get the value of child from

xmlstr = """<p:root xmlns:p="http://tempuri.org/string"><p:child
DataType="String">Hellpppp</p:child></p:root>"""

using
doc=parseString(xmlstr)
nodeList = xml.xpath.Evaluate("/p:root/p:child/text()", doc)

and am getting the following exception:

xml.xpath.RuntimeException: Undefined namespace prefix: "p".

I am using python 2.2.2 with PyXML 0.8

I think my usage of the Evaluate is wrong or incomplete. I have tried
to google for information but to no avail. Can anyone please shed
light on this.

Thanks
Bipin
Jul 18 '05 #1
6 5815
bv*****@yahoo.com (0wl) wrote in message news:<de**************************@posting.google. com>...
Hi,

I am trying to get the value of child from

xmlstr = """<p:root xmlns:p="http://tempuri.org/string"><p:child
DataType="String">Hellpppp</p:child></p:root>"""

using
doc=parseString(xmlstr)
nodeList = xml.xpath.Evaluate("/p:root/p:child/text()", doc)

and am getting the following exception:

xml.xpath.RuntimeException: Undefined namespace prefix: "p".


The problem is that the XPath query engine doesn't know what the
prefix "p" is, and it won't automatically deduce it from your XML
document. In other words, the prefixes used in your query are
effectively independent from those used in your document, although
this does give you the luxury of changing either your query or your
document without having to go through the other adjusting the prefixes
to match.

Try this:
c = xml.xpath.Context.Context(doc)
c.setNamespaces({"p" : "http://tempuri.org/string"})
This makes a context and then adds the definition of the prefix for
the XPath query engine. I think xml.xpath.CreateContext(doc) may be
more appropriate, but I always use the above style. Also, you could
probably specify the prefix/namespace definitions in the Context
constructor, but this is just as easy.
e = xml.xpath.Compile("/p:root/p:child/text()")
I compile the expression in order to allow the context to be used. We
need a context because there apparently isn't any way of specifying
the prefix/namespace definitions directly in an Evaluate call.
Therefore, we have to set up a context first to contain those
definitions.
e.evaluate(c)

[<DOM Text node "Hellpppp">]

Yes, it works! ;-)

Paul
Jul 18 '05 #2
> [Paul Boddie]
...
Try this:
c = xml.xpath.Context.Context(doc)
c.setNamespaces({"p" : "http://tempuri.org/string"}) This makes a context and then adds the definition of the prefix for
the XPath query engine... e = xml.xpath.Compile("/p:root/p:child/text()") I compile the expression in order to allow the context to be used.
... we have to set up a context first to contain those
definitions. e.evaluate(c)

[<DOM Text node "Hellpppp">]


A very nice and helpful Paul Boddie in action on c.l.p.

But OMFG! Here we see why we need a good *high* level XML library in
the Python Standard Library. The effbot is doing great work with
elementtree at www.effbot.org, but he is doing that all alone. I
think a good high level XML library should have a very high priority
for Python.

It should be a much higher priority for the core Python developers
than the extensions I have seen lately. Booleans, bah! And for
instance, I hate it to make my code unreadable using list
comprehensions and other syntactic sugar; and then later having to
explain it to a C programmer. "Ha!" she says, "You claimed the Python
language reads like pseudocode!". Geez.
Jul 18 '05 #3
0wl
Works like a charm!!!!!

My Ignorance shines bright.... :-).

Anywhere I can read about this stuff..

Thanks
--Bipin.

pa**@boddie.net (Paul Boddie) wrote in message news:<23*************************@posting.google.c om>...
bv*****@yahoo.com (0wl) wrote in message news:<de**************************@posting.google. com>...
Hi,

I am trying to get the value of child from

xmlstr = """<p:root xmlns:p="http://tempuri.org/string"><p:child
DataType="String">Hellpppp</p:child></p:root>"""

using
doc=parseString(xmlstr)
nodeList = xml.xpath.Evaluate("/p:root/p:child/text()", doc)

and am getting the following exception:

xml.xpath.RuntimeException: Undefined namespace prefix: "p".


The problem is that the XPath query engine doesn't know what the
prefix "p" is, and it won't automatically deduce it from your XML
document. In other words, the prefixes used in your query are
effectively independent from those used in your document, although
this does give you the luxury of changing either your query or your
document without having to go through the other adjusting the prefixes
to match.

Try this:
c = xml.xpath.Context.Context(doc)
c.setNamespaces({"p" : "http://tempuri.org/string"})
This makes a context and then adds the definition of the prefix for
the XPath query engine. I think xml.xpath.CreateContext(doc) may be
more appropriate, but I always use the above style. Also, you could
probably specify the prefix/namespace definitions in the Context
constructor, but this is just as easy.
e = xml.xpath.Compile("/p:root/p:child/text()")
I compile the expression in order to allow the context to be used. We
need a context because there apparently isn't any way of specifying
the prefix/namespace definitions directly in an Evaluate call.
Therefore, we have to set up a context first to contain those
definitions.
e.evaluate(c)

[<DOM Text node "Hellpppp">]

Yes, it works! ;-)

Paul

Jul 18 '05 #4
hw***@hotmail.com (Will Stuyvesant) wrote in message news:<cb**************************@posting.google. com>...

A very nice and helpful Paul Boddie in action on c.l.p.
Glad to be of some help. ;-)
But OMFG!
Object Management F<something> Group?
Here we see why we need a good *high* level XML library in
the Python Standard Library. The effbot is doing great work with
elementtree at www.effbot.org, but he is doing that all alone. I
think a good high level XML library should have a very high priority
for Python.
This is argued for a lot, but I don't really see total acceptance
until the PyXML people get on board. They seem to be managing well
enough, and whilst one might argue that the PyXML APIs are too
highbrow for the rest of the community, no-one using PyXML for serious
XML processing is going to adopt the mythical "Pythonic" API that
everyone is talking about unless it does the business for them as
well.
It should be a much higher priority for the core Python developers
than the extensions I have seen lately. Booleans, bah!
I don't want to get dragged into another debate on core language
enhancements. :-) I suppose once Python 2.3 is released, we may well
see more focus on the libraries.
And for instance, I hate it to make my code unreadable using list
comprehensions and other syntactic sugar; and then later having to
explain it to a C programmer. "Ha!" she says, "You claimed the Python
language reads like pseudocode!". Geez.


Actually, list comprehensions are very readable... to a mathematician.
Seriously, though, I'm using them a lot more than map/filter/reduce
these days, but I can't see a real need for the language runtime to go
beyond generators for the time being, although I'll surely get branded
as being "short-sighted" for saying that.

Paul
Jul 18 '05 #5
bv*****@yahoo.com (0wl) wrote in message news:<de*************************@posting.google.c om>...
Works like a charm!!!!!

My Ignorance shines bright.... :-).
Or perhaps the documentation doesn't? ;-)
Anywhere I can read about this stuff..


I think I picked up on this stuff by reading the W3C specification and
browsing the xml.xpath sources. You could do some investigation in
Python 2.2 by using the built-in help command; for example:

help(xml.xpath)

And the specification for XPath 1.0 is found here:

http://www.w3.org/TR/xpath

Perhaps a tutorial is in order.

Paul
Jul 18 '05 #6
Paul Boddie wrote:

hw***@hotmail.com (Will Stuyvesant) wrote in message news:<cb**************************@posting.google. com>...

A very nice and helpful Paul Boddie in action on c.l.p.


Glad to be of some help. ;-)
But OMFG!


Object Management F<something> Group?


First words are "Oh My", while last word references one's
personal deity. The F word is left to personal choice as
well. ;-)

-Peter
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by bdinmstig | last post: by
1 post views Thread by bdinmstig | last post: by
1 post views Thread by Dave | last post: by
10 posts views Thread by Michael C# | last post: by
1 post views Thread by bruce | last post: by
1 post views Thread by bruce | last post: by
2 posts views Thread by bruce | last post: by
reply views Thread by John Krukoff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.