[XML-SIG] Where is the documentation?

paul.boddie at ementor.no paul.boddie at ementor.no
Thu Jul 17 14:50:04 EDT 2003


Luiz Siqueira Neto [mailto:cybersamurai at mac.com] wrote:
> 
> I can't find a simple documentation to learn how I use XPath, DOM, SAX in 
> Python. I use XPath in Java and it is very easy. I believe XPath in Python is 
> very easy too but I need some good start point. If some body have some idea 
> or URL will be wellcome.  : )

Using PyXML, try opening a document:

  import xml.dom.minidom
  document = xml.dom.minidom.parse(some_file_or_filename)

Then, use the XPath features of PyXML:

  import xml.xpath
  elements = xml.xpath.Evaluate(some_xpath_query, document)

Obviously, you'll need to define 'some_xpath_query' using the XPath notation:

  http://www.w3.org/TR/xpath

Like the Jaxen API for Java (if that's what you mean by "XPath in Java"),
you can compile/prepare XPath expressions in advance:

  expr = xml.xpath.Compile(some_xpath_query)
  # In Jaxen: expr = org.jaxen.dom.DOMXPath(some_xpath_query)

It appears that you need to wrap your document up in an XPath context before
you can evaluate that expression:

  context = xml.xpath.CreateContext(document)
  # Or: context = xml.xpath.Context.Context(document)

You can add namespace prefix definitions and variable definitions to the
context either during its initialisation or by using certain methods:

  context.setVarBindings(
    {
      (some_ns_often_None, some_variable) : some_value,
      # ...
    })

Then you can do the evaluation:

  elements = expr.evaluate(context)
  # In Jaxen: elements = expr.selectNodes(context)
  # Although Jaxen uses the document directly, I think.

Use document.toxml() or xml.dom.ext.PrettyPrint(document) to write the file.

Good luck!

Paul



More information about the XML-SIG mailing list