[XML-SIG] Using XML Namespaces in XPath Expressions from Python

Uche Ogbuji uche.ogbuji at fourthought.com
Mon Jun 13 17:33:26 CEST 2005


On Mon, 2005-06-13 at 10:29 -0400, JonTom Kittredge wrote:
> Uche, Thanks for your response. My comments are below.

> On 2005-06-09 at 06:26, Uche Ogbuji wrote:
> >  On Wed, 2005-06-08 at 10:27 -0400, JonTom Kittredge wrote:
> >  > 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)
> >  
> >  Where are you passing in the NS mapping here?  Did I miss a PyXML XPath
> >  update that defaults mappings to in-scope namespaces on the node passed
> >  in?
> 
> That's the nub of my question. How do I pass namespace mappings into
> Evaluate()? 

outContext = Context(inputDom.documentElement, processorNss={u'dbq':
DBQNS})

> The only thing I could see to try, from looking at the documentation,
> was to use the context argument. The Context object holds namespace
> objects, so I thought that that might be the way to pass in my
> definition. 

Yes, but you have to give it the namespace defs, as above.

> As you can see from the output, the Context I get from the document
> node already has the "dbq" namespace defined (since it appears in the
> input), before I call Evaluate(). However, it apparently gets reset
> during the call to Evaluate().

That's what's confusing me.  I'm not sure why it automatically has that
namespace at any point.  Anyway, see if my suggestion helps.


-- 
Uche Ogbuji                               Fourthought, Inc.
http://uche.ogbuji.net                    http://fourthought.com
http://copia.ogbuji.net                   http://4Suite.org
Use CSS to display XML, part 2 - http://www-128.ibm.com/developerworks/edu/x-dw-x-xmlcss2-i.html
XML Output with 4Suite & Amara - http://www.xml.com/pub/a/2005/04/20/py-xml.html
Use XSLT to prepare XML for import into OpenOffice Calc - http://www.ibm.com/developerworks/xml/library/x-oocalc/
Schema standardization for top-down semantic transparency - http://www-128.ibm.com/developerworks/xml/library/x-think31.html



More information about the XML-SIG mailing list