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.

Browsing hierarchical directory structure

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 ...

Get Building Oracle XML Applications 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.