# Implementation of xpath in ruby?

**URL:** <https://www.ruby-forum.com/t/implementation-of-xpath-in-ruby/138378>\
**Category:** Ruby\
**Created:** [May 22, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/implementation-of-xpath-in-ruby/138378 "2008-05-22T13:07:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [May 22, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/implementation-of-xpath-in-ruby/138378/1 "2008-05-22T13:07:58Z")

</div>

Dear Friends,

i need to implement xpath in my parser class. I have implemented a new  
parser which parses xml document. I have used token types to parse xml  
like,

while parser.next

```
     if parser.token_type == tag_start
        parser.tag
     end

     if parser.token_type == tag_end
        parser.tag_end
     end
end

```

so a token\_type method returns token types like tag\_start, tag\_end,  
comment, pi etc and tag method returns the tag start and tag\_end method  
returns the tag end of an xml document.

now i need to implement xpath in my parser class. I need to navigate the  
xml document using xpath like,

while parser.next!

parser.path( ‘book/book-name’ ) do

end

```
end

```

could anyone help me with some solution to implement xpath in my parser  
class.I don’t want to use REXML or HPRICOT. i want to create my own  
methods which does the above.

Thanks in advance

Regards,  
Martin

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [May 22, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/implementation-of-xpath-in-ruby/138378/2 "2008-05-22T17:49:22Z")

</div>

On Thu, May 22, 2008 at 1:08 PM, dare ruby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> end
> 
> ```
> 
> now i need to implement xpath in my parser class. I need to navigate the  
> could anyone help me with some solution to implement xpath in my parser  
> class.I don’t want to use REXML or HPRICOT. i want to create my own  
> methods which does the above.

I’m not sure about your interface, anyway:  
You probably need to keep info about the current path from the root  
(stack is usually handy for this), and  
have an xpath parser that says yes/no for an xpath expression and  
current path.  
then you yield the current node.

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [May 22, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/implementation-of-xpath-in-ruby/138378/3 "2008-05-22T19:50:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [May 22, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/implementation-of-xpath-in-ruby/138378/4 "2008-05-22T19:59:04Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Mark T. wrote:  
| Why are you intentionally reinventing the wheel?

Learning exercise, for example.

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ The dame was hysterical. Dames Usually are. – Calvin as Tracer  
Bullet  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkg1tFoACgkQbtAgaoJTgL8HSACaAkMA7h2jBYo5Wh3vLDqTxb7J  
jqcAn0GGSm5pElioFycmVMVmM9bMaUbS  
=hAvY  
-----END PGP SIGNATURE-----
