Trailblazing with XPath
By Michael Floyd
When he was young, my brother Greg was into motorcross. He received his first motorcycle, a Yamaha 125, when he was just 13. He subsequently rode that bike in the legendary Barstow to Las Vegas race featured in the movie, "On Any Sunday." Anxious to show me the rigors of the course, he one day insisted that I accompany him on an enduro ride. We drove through the Mojave Desert and by evening found a dry lake where we set up camp. The next morning, we unloaded our motocross bikes and were off like a shot. I spent half the day riding, and he spent the other half picking me up off the desert floor.
As the sun began to set, we focused our attention on returning to camp. The problem was, everything in the desert looked the same. "Which way is camp?" I asked. Somehow, Greg spotted a path we'd crossed earlier in the day and we found our camp about an hour after dark. Greg had an amazing internal compass that always showed him the way home.
As in desert enduro, finding your way through XML documents isn't always a straightforward task. Fortunately, the designers of XML have included a mechanism, called XPath, that helps you navigate through documents. XPath partly defines a syntax that lets you easily traverse a tree's structure and select one or more of its nodes. Once you've selected a node or nodes, you can manipulate, reorder, or transform them in any way you desire. The mechanism that lets you select tree nodes is called a pattern. A pattern is actually a limited form of what XPath calls location paths. (We'll get to location paths in a moment.)
Much of XPath's expression language was originally described in the early XSL specification.