472,951 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

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 5919
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bdinmstig | last post by:
I am building various framework components for my team to use in development, and one of those components is a Facade for reading/writing user preferences. The idea is that preference settings...
1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
1
by: Dave | last post by:
Is it possible to get <codes><code id="4"><name>abc</name></code></codes from the XML below in single SelectSingleNode/xPath expression step OR is going to have to be a multi=step process of...
10
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
2
by: nick_tucker | last post by:
Hi, I am very new to XML and XPATH. I have made a sample XML fileto ask my question. <?xml version="1.0" encoding="utf-8"?> <Test> <A> <B> <C> <D>
1
by: bruce | last post by:
hi... 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...
2
by: luthriaajay | last post by:
I intend using XPATH to browsw thru an XML document in Java. This XML document is of type Document. First Question: How do I use the Document Builder to open an XML document? Document...
1
by: bruce | last post by:
Hi. Got a test web page, that basically has two "<html" tags in it. Examining the page via Firefox/Dom Inspector, I can create a test xpath query "/html/body/form" which gets the target form for...
2
by: bruce | last post by:
morning.... i apologize up front as this is really more of an xpath question.. in my python, i'm using the xpath function to iterate/parse some html. i can do something like ...
0
by: John Krukoff | last post by:
On Wed, 2008-09-03 at 13:36 -0700, bruce wrote: Well, you could just do the test (and the count!) in the xpath expression: count( //tr/td ) It sounds like you're not familiar with xpath? I...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.