Searching XML with XPath
When you organize files into directories
on a disk, you use the directories to give some meaningful structure
to your files. Familiar commands like dir or
ls allow you to search for matching files that
have been organized into those nested directories. Consider the
directory hierarchy of drive M: as shown in Figure 2.9 using the Windows Explorer.

Figure 2-9. Browsing hierarchical directory structure
In order to search this hierarchy, you use a
path notation to
indicate what files you are trying to find. The path notation for
searching a Unix filesystem, for example, allows you to use the
notation /MovieList/Movie/Cast to refer to the
Cast subdirectory under the
Movie subdirectory of
MovieList, or /MovieList/Movie/Cast/*
to refer to any file in that
directory.
To list all the files in the Actor subdirectory,
you can issue the command:
ls /MovieList/Movie/Cast/Actor/*
which produces this list of matching files:
/MovieList/Movie/Cast/Actor/First /MovieList/Movie/Cast/Actor/Last /MovieList/Movie/Cast/Actor/Award
As we saw in Chapter 1, XML documents also have an industry-standard path notation for searching their content, called XPath. You’ll find its syntax easy to understand because of its basic similarity to what we saw earlier for files in directories. We’ll see with several examples (and throughout this book) that the searches you can do with the XPath notation ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access