Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Using xml.xpath question.

Reply
Thread Tools

Using xml.xpath question.

 
 
0wl
Guest
Posts: n/a
 
      07-10-2003
Hi,

I am trying to get the value of child from

xmlstr = """<p:root xmlns="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
 
Reply With Quote
 
 
 
 
Paul Boddie
Guest
Posts: n/a
 
      07-11-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (0wl) wrote in message news:<(E-Mail Removed). com>...
> Hi,
>
> I am trying to get the value of child from
>
> xmlstr = """<p:root xmlns="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
 
Reply With Quote
 
 
 
 
Will Stuyvesant
Guest
Posts: n/a
 
      07-11-2003
> [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.
 
Reply With Quote
 
0wl
Guest
Posts: n/a
 
      07-11-2003
Works like a charm!!!!!

My Ignorance shines bright.... .

Anywhere I can read about this stuff..

Thanks
--Bipin.

(E-Mail Removed) (Paul Boddie) wrote in message news:<(E-Mail Removed) om>...
> (E-Mail Removed) (0wl) wrote in message news:<(E-Mail Removed). com>...
> > Hi,
> >
> > I am trying to get the value of child from
> >
> > xmlstr = """<p:root xmlns="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

 
Reply With Quote
 
Paul Boddie
Guest
Posts: n/a
 
      07-14-2003
(E-Mail Removed) (Will Stuyvesant) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
Paul Boddie
Guest
Posts: n/a
 
      07-14-2003
(E-Mail Removed) (0wl) wrote in message news:<(E-Mail Removed) 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
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      07-14-2003
Paul Boddie wrote:
>
> (E-Mail Removed) (Will Stuyvesant) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a Link Button to redirect to another page by using data from =?Utf-8?B?R1REcml2ZXI=?= ASP .Net 1 02-16-2005 07:04 PM
Error page using the Application_Error void dosnt work when using DIV for a please wait message s_erez@hotmail.com ASP .Net 2 12-24-2004 12:11 PM
no code in webform using vs.net, but in webform using notepad timmso ASP .Net 1 12-12-2003 04:30 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments