Using xml.xpath question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 0wl

    Using xml.xpath question.

    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
  • Paul Boddie

    #2
    Re: Using xml.xpath question.

    [email protected] om (0wl) wrote in message news:<de5c7cc0. 0307101347.2c70 [email protected] ogle.com>...[color=blue]
    > 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".[/color]

    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:
    [color=blue][color=green][color=darkred]
    >>> c = xml.xpath.Conte xt.Context(doc)
    >>> c.setNamespaces ({"p" : "http://tempuri.org/string"})[/color][/color][/color]

    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.
    [color=blue][color=green][color=darkred]
    >>> e = xml.xpath.Compi le("/p:root/p:child/text()")[/color][/color][/color]

    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.
    [color=blue][color=green][color=darkred]
    >>> e.evaluate(c)[/color][/color][/color]
    [<DOM Text node "Hellpppp">]

    Yes, it works! ;-)

    Paul

    Comment

    • Will Stuyvesant

      #3
      Re: Using xml.xpath question.

      > [Paul Boddie][color=blue]
      > ...
      > Try this:[color=green][color=darkred]
      > >>> c = xml.xpath.Conte xt.Context(doc)
      > >>> c.setNamespaces ({"p" : "http://tempuri.org/string"})[/color][/color]
      > This makes a context and then adds the definition of the prefix for
      > the XPath query engine...[color=green][color=darkred]
      > >>> e = xml.xpath.Compi le("/p:root/p:child/text()")[/color][/color]
      > 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.[color=green][color=darkred]
      > >>> e.evaluate(c)[/color][/color]
      > [<DOM Text node "Hellpppp">][/color]

      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.

      Comment

      • 0wl

        #4
        Re: Using xml.xpath question.

        Works like a charm!!!!!

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

        Anywhere I can read about this stuff..

        Thanks
        --Bipin.

        [email protected] (Paul Boddie) wrote in message news:<23891c90. 0307110057.bbe9 [email protected] gle.com>...[color=blue]
        > [email protected] om (0wl) wrote in message news:<de5c7cc0. 0307101347.2c70 [email protected] ogle.com>...[color=green]
        > > 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".[/color]
        >
        > 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:
        >[color=green][color=darkred]
        > >>> c = xml.xpath.Conte xt.Context(doc)
        > >>> c.setNamespaces ({"p" : "http://tempuri.org/string"})[/color][/color]
        >
        > 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.
        >[color=green][color=darkred]
        > >>> e = xml.xpath.Compi le("/p:root/p:child/text()")[/color][/color]
        >
        > 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.
        >[color=green][color=darkred]
        > >>> e.evaluate(c)[/color][/color]
        > [<DOM Text node "Hellpppp">]
        >
        > Yes, it works! ;-)
        >
        > Paul[/color]

        Comment

        • Paul Boddie

          #5
          Re: Using xml.xpath question.

          [email protected] om (Will Stuyvesant) wrote in message news:<cb035744. 0307110917.7459 [email protected] ogle.com>...[color=blue]
          >
          > A very nice and helpful Paul Boddie in action on c.l.p.[/color]

          Glad to be of some help. ;-)
          [color=blue]
          > But OMFG![/color]

          Object Management F<something> Group?
          [color=blue]
          > 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.[/color]

          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.
          [color=blue]
          > It should be a much higher priority for the core Python developers
          > than the extensions I have seen lately. Booleans, bah![/color]

          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.
          [color=blue]
          > 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.[/color]

          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

          Comment

          • Paul Boddie

            #6
            Re: Using xml.xpath question.

            [email protected] om (0wl) wrote in message news:<de5c7cc0. 0307111230.7d83 [email protected] gle.com>...[color=blue]
            > Works like a charm!!!!!
            >
            > My Ignorance shines bright.... :-).[/color]

            Or perhaps the documentation doesn't? ;-)
            [color=blue]
            > Anywhere I can read about this stuff..[/color]

            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:

            The World Wide Web Consortium (W3C) is an international community where Member organizations, a full-time staff, and the public work together to develop Web standards.


            Perhaps a tutorial is in order.

            Paul

            Comment

            • Peter Hansen

              #7
              Re: Using xml.xpath question.

              Paul Boddie wrote:[color=blue]
              >
              > [email protected] om (Will Stuyvesant) wrote in message news:<cb035744. 0307110917.7459 [email protected] ogle.com>...[color=green]
              > >
              > > A very nice and helpful Paul Boddie in action on c.l.p.[/color]
              >
              > Glad to be of some help. ;-)
              >[color=green]
              > > But OMFG![/color]
              >
              > Object Management F<something> Group?[/color]

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

              -Peter

              Comment

              Working...