Why are you intentionally reinventing the wheel? Is it because you want a stream parser (like SAX) rather than building a DOM? Unfortunately, XPath requires building a DOM. Is it that you need it to be fast? A fast implementation of XPath is in libxml. Perhaps you can try the ruby binding to libxml. It's a work in progress but I have used it successfully to parse XML (and it's lightning-fast).