9.13. Some Examples

At the beginning of this chapter, we saw three examples of searches that require hierarchical context. In the short sections below, solutions to these three queries are illustrated.

9.13.1. Find “Sean” Anywhere Within a Name Element

We use the following small staff file to illustrate this query.

CD-ROM reference=9035.txt
C>type staff.xml

<staff>
<department name="Technical">
<person>
<title>Technical Director</title>
<name>
<given>Sean</given>
<family>McGrath</family>
</name>
<email>Sean@digitome.com</email>
<web>http://www.digitome.com/Sean.html</web>
</person>
</department>
</staff>

We can use the xgrep –ancestor switch to restrict the match for “Sean” to be within name elements only:

CD-ROM reference=9036.txt

C>python ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.