473,756 Members | 5,656 Online
Bytes | Software Development & Data Engineering Community
+ 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:chil d
DataType="Strin g">Hellpppp</p:child></p:root>"""

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

and am getting the following exception:

xml.xpath.Runti meException: 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 5982
bv*****@yahoo.c om (0wl) wrote in message news:<de******* *************** ****@posting.go ogle.com>...
Hi,

I am trying to get the value of child from

xmlstr = """<p:root xmlns:p="http://tempuri.org/string"><p:chil d
DataType="Strin g">Hellpppp</p:child></p:root>"""

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

and am getting the following exception:

xml.xpath.Runti meException: 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.Conte xt.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.Creat eContext(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.Compi le("/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.Conte xt.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.Compi le("/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.goo gle.com>...
bv*****@yahoo.c om (0wl) wrote in message news:<de******* *************** ****@posting.go ogle.com>...
Hi,

I am trying to get the value of child from

xmlstr = """<p:root xmlns:p="http://tempuri.org/string"><p:chil d
DataType="Strin g">Hellpppp</p:child></p:root>"""

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

and am getting the following exception:

xml.xpath.Runti meException: 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.Conte xt.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.Creat eContext(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.Compi le("/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.c om (Will Stuyvesant) wrote in message news:<cb******* *************** ****@posting.go ogle.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.c om (0wl) wrote in message news:<de******* *************** ***@posting.goo gle.com>...
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.c om (Will Stuyvesant) wrote in message news:<cb******* *************** ****@posting.go ogle.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
1823
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 are stored in a free-format XML document (in memory for the life of the session) and persisted to a database (as free text) on exit. I have taught my developers the basics of XPath, however I don't want to have to review XMLDOM code all over the...
1
6824
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 elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
1
4004
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 using SelectNodes with an XPath expression to return all the nodes with "code id = 4", iterate the NodeList, re-build the XML string and wrap it with "<codes>" again? I wasn't sure if you can somehow return the parent node when using xPath to find...
10
2287
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 field names. Everything there works fine. Here's my XML Document: <?xml version="1.0" standalone="yes" ?> <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd"> <tblItem>
2
2435
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
1663
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 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...
2
1170
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 xmlMessage; DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
1
2218
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 the test. The issue comes when I examine the page's source html. It looks like: <html> <body> </body>
2
1378
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 s=d.xpath("//tr/td/text()") count=len(s)
0
1061
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 would recommend the O'Reilly XSLT book, it has an excellent introduction to xpath in chapter 3.
0
9271
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9868
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9707
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5139
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
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 we have to send another system
2
3352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2664
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.