Using XML Namespaces in XPath Expressions from Python

JonTom Kittredge <[email protected]>
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
I'm having trouble using PyXML to evaluate XPath expressions that
include namespace references.

Here is a sample Python snipped:

    #!/usr/bin/env pystart

    import sys
    import xml.dom.minidom

    from xml.xpath.Context import Context
    from xml.xpath import Evaluate
    from xml.xpath.NamespaceNode import NamespaceNode


    if __name__ == "__main__":
      sys.stderr.write("XPathTest: 18\n")

      inputDom = xml.dom.minidom.parse(open("xpathtest-input.xml"))

      sys.stderr.write("Node: %s\n" % (inputDom.documentElement))
      outContext = Context(inputDom.documentElement)
      sys.stderr.write("NSes=%s\n" % (outContext.nss()))

      result = Evaluate("count(/dbq:Contract)",
                        contextNode=inputDom.documentElement,
                        context=outContext)
      sys.stderr.write("Eval xpath \"count(/dbq:Contract)\": %d\n" % result)

Here is what xpathtest-input.xml looks like:

    <?xml version="1.0"?>

    <dbq:Contract
        xmlns:dbq="http://dtd.itasoftware.com/Dubuque">
      <dbq:RuleContent/>
    </dbq:Contract>

Here is the output I get

[xsl] zyp| xpathtest.py                              
XPathTest: 18
Node: <DOM Element: dbq:Contract at 0x82a5a6c>
NSes={'xml': 'http://www.w3.org/XML/1998/namespace', u'dbq': u'http://dtd.itasoftware.com/Dubuque'}
Traceback (most recent call last):
  File "xpathtest.py", line 22, in ?
    context=outContext)
  File "/ita/python-2.2.2/lib/python2.2/site-packages/_xmlplus/xpath/__init__.py", line 70, in Evaluate
    retval = parser.new().parse(expr).evaluate(con)
  File "/ita/python-2.2.2/lib/python2.2/site-packages/_xmlplus/xpath/ParsedExpr.py", line 179, in evaluate
    arg0 = self._arg0.evaluate(context)
  File "/ita/python-2.2.2/lib/python2.2/site-packages/_xmlplus/xpath/ParsedAbsoluteLocationPath.py", line 26, in evaluate
    rt = self._child.select(context)
  File "/ita/python-2.2.2/lib/python2.2/site-packages/_xmlplus/xpath/ParsedStep.py", line 32, in evaluate
    (node_set, reverse) = self._axis.select(context, self._nodeTest.match)
  File "/ita/python-2.2.2/lib/python2.2/site-packages/_xmlplus/xpath/ParsedAxisSpecifier.py", line 114, in select
    list(context.node.childNodes))
  File "/ita/python-2.2.2/lib/python2.2/site-packages/_xmlplus/xpath/ParsedAxisSpecifier.py", line 112, in <lambda>
    rt = filter(lambda node, test=nodeTest, context=context, pt=self.principalType:
  File "/ita/python-2.2.2/lib/python2.2/site-packages/_xmlplus/xpath/ParsedNodeTest.py", line 168, in match
    self._prefix)
xml.xpath.RuntimeException: Undefined namespace prefix: "dbq".
exit status: 1


I can see from the output that the Context object I am passing to
Evaluate() includes the DBQ namespace. But apparently that is not
enough.

Can anyone tell me what I am doing wrong?

Yours, JonTom

       JT Kittredge
       ITA Software, Inc.
       Cambridge, Massachusetts
_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.