[XML-SIG] 'searching' XML documents to extract 'chunks' of XML

Dieter Maurer dieter@handshake.de
Fri, 12 Mar 1999 20:10:12 +0000 (/etc/localtime)

Tony McDonald writes:
 > Andrew, this is *a real help*. Thanks for the information, now all I 
 > need to do is to figure out how to plumb the DOM into the XSL module 
 > of Dieters' and I *think* I'm away.
Section 3 of URL:http://www.handshake.de/~dieter/pyprojects/xslpattern.html
contains a small partial example.

You use the "Parser" object to transform an XSL pattern string
into an XSL pattern object. Pattern objects have several methods,
the most essential of which are "select" and "match".
Both get a DOM node as first parameter. "select" returns the list
of DOM nodes selected by the pattern with the node as context
(this is defined in the XSL working draft spec
With "match", you can check whether the node is matched by the pattern
(defined in the XSL spec, too).

Suppose you have build a DOM tree in "domtree" (e.g. in the way
Andrew has pointed out). Suppose, you want to select all
"chapters" in the tree.
You build the pattern object:

	p= Parser('//chapter')

	chapters= p.select(domtree)
gives you the list of "chapter" nodes in the tree.

If you would like to access a required "no" attribute of the
first chapter, you can use:
	no= Parser('@no').select(chapters[0])[0]
(however, you probably would use PyDOM for this directly).

A more complex example would be to select the "table" with
"no=1" in the chapter with "no=3" (first table in third chapter):

	table= Parser('//table[@no="1" and ancestor(chapter[@no="3"])]').select(domtree)[0]

or equivalently (but more efficiently):

	table= Parser('//chapter[@no="3"]//table[@no="1"]').select(domtree)[0]

Good luck
- Dieter