In this chapter, we use the Evaluate
function from the 4XPath API to apply
XPath expressions against node sets. For programmatic use of XPath
within Python, the 4XPath API is readily available and offers
considerable power.
Most of the XPath API is geared towards supporting XPath expressions, as XPath is a standard. But for the programmer embedding XPath processing functionality into their applications, there is some optimization found in 4XPath.
The Compile
and
Context
functions aid the developer
to create compiled XPath expressions for repeated use against multiple
documents. For example, if you are accepting large numbers of XML
documents from customers or suppliers, you may want to apply an XPath
expression to each one (as it arrives) to figure out what to do with it,
or where to route it within your organization. Having your XPath
expression readily compiled and applied against each unique document
adds speed to your application, as you’ve done away with the need to
parse the XPath expression.
The Compile
function returns an
expression object that supports an evaluate
method similar to the Evaluate
function used thus far in this
chapter. However, the method expects a Context
object, not a node. The task of
compiling an expression, and then using the compiled version, is fairly
simple:
expression = Compile("ship/@name") context = Context(dom.documentElement) nodes = expression.evaluate(context)
The first step is to generate an expression; the second step is to
generate context for the document or node set you’re working with. You
can run the expression by calling the evaluate
method of the compiled expression
object, as demonstrated in Example
5-6 (which makes use of the ships.xml file).
#!/usr/local/bin/python import sys from xml.dom.ext.reader import PyExpat from xml.xpath import Compile from xml.xpath.Context import Context reader = PyExpat.Reader( ) dom = reader.fromStream(sys.stdin) expression = Compile("ship/@name") context = Context(dom.documentElement) nodes = expression.evaluate(context) print "Nodes: ", nodes
When executed from the command line with ships.xml as input, the program generates the following output:
$ python compx.py < ships.xml Nodes: [<Attribute Node at a1cd9c: Name="name", Value="USS Enterprise">, <Attribute Node at a2305c: Name="name", Value="USS Voyager">, <Attribute Node at a2b7fc: Name="name", Value="USS Enterprise">, <Attribute Node at a33fdc: Name="name", Value="USS Enterprise">, <Attribute Node at a3a30c: Name="name", Value="USS Sao Paulo">]
Your Python and XML toolkit is almost complete; we look at one more core technology in the next chapter. After that, we delve into topics that deal with actually integrating XML with your existing systems and building new systems using Python and XML.
3.14.141.115