473,466 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 5951
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.