# Getting XPath in REXML to dive deeper

**URL:** <https://rubytalk.org/t/getting-xpath-in-rexml-to-dive-deeper/40879>\
**Category:** ruby-talk\
**Created:** [24 September 2007 19:27 UTC](https://rubytalk.org/t/getting-xpath-in-rexml-to-dive-deeper/40879 "2007-09-24T19:27:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [24 September 2007 19:27 UTC](https://rubytalk.org/t/getting-xpath-in-rexml-to-dive-deeper/40879/1 "2007-09-24T19:27:50Z")

</div>

Hello,  
I'm finally using REXML instead of plain vanilla Ruby to convert XML  
data. I've learned a bit about XPath and how REXML can use it. I've got  
a sample XML file whose root element is \<registration\>. Underneath that  
element are a bunch of children elements. So, when I do the instruction  
listed below, I get all of them. But, I don't get the last two elements  
that, to me anyway, are children, too. The only thing different about  
them is that they have children, too. Does anyone know the nomenclature  
for getting ALL of the children of an element, not just those that are  
immediate children. I guess I want grandkids, too!

XPath.each(doc, "//registration/\*") { |element| puts element.text }

Thanks,  
Peter

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [24 September 2007 19:34 UTC](https://rubytalk.org/t/getting-xpath-in-rexml-to-dive-deeper/40879/2 "2007-09-24T19:34:55Z")

</div>

Peter Bailey wrote:

> I'm finally using REXML instead of plain vanilla Ruby to convert XML  
> data. I've learned a bit about XPath and how REXML can use it. I've got  
> a sample XML file whose root element is \<registration\>. Underneath that  
> element are a bunch of children elements. So, when I do the instruction  
> listed below, I get all of them. But, I don't get the last two elements  
> that, to me anyway, are children, too. The only thing different about  
> them is that they have children, too. Does anyone know the nomenclature  
> for getting ALL of the children of an element, not just those that are  
> immediate children. I guess I want grandkids, too!
> 
> XPath.each(doc, "//registration/\*") { |element| puts element.text }

The term you are looking for is 'descendants'. In XPath, '/' is  
shorthand for the 'child' axis. You want the 'descendant' axis, which  
has the shorthand (that you already used) of '//'.

So "//registration//\*" is "At any level, find an element named  
'registration', and then find every element below any of them."

You possible want "registration//\*" which is "every element in the  
entire XML file at every level except for the root node", but that seems  
rather pointless. (Why have a hierarchy in the XML if you're throwing  
away the meaning?) Perhaps that will work for you, or perhaps you should  
think further about exactly what elements you really want to traverse.

Hope that helps (oh, and this has nothing to do with Ruby, BTW :p)  
Gavin

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mark\_Volkmann](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Mark\_Volkmann](https://rubytalk.org/u/Mark_Volkmann)\
**Post date:** [24 September 2007 19:41 UTC](https://rubytalk.org/t/getting-xpath-in-rexml-to-dive-deeper/40879/3 "2007-09-24T19:41:38Z")

</div>

Doesn't he want "/registration//\*" ? Note the slash at the beginning.

> **···**
>
> On Sep 24, 2007, at 2:34 PM, Gavin Kistner wrote:
> 
> > Peter Bailey wrote:
> > 
> > > I'm finally using REXML instead of plain vanilla Ruby to convert XML  
> > > data. I've learned a bit about XPath and how REXML can use it. I've got  
> > > a sample XML file whose root element is \<registration\>. Underneath that  
> > > element are a bunch of children elements. So, when I do the instruction  
> > > listed below, I get all of them. But, I don't get the last two elements  
> > > that, to me anyway, are children, too. The only thing different about  
> > > them is that they have children, too. Does anyone know the nomenclature  
> > > for getting ALL of the children of an element, not just those that are  
> > > immediate children. I guess I want grandkids, too!
> > > 
> > > XPath.each(doc, "//registration/\*") { |element| puts element.text }
> > 
> > The term you are looking for is 'descendants'. In XPath, '/' is  
> > shorthand for the 'child' axis. You want the 'descendant' axis, which  
> > has the shorthand (that you already used) of '//'.
> > 
> > So "//registration//\*" is "At any level, find an element named  
> > 'registration', and then find every element below any of them."
> > 
> > You possible want "registration//\*" which is "every element in the  
> > entire XML file at every level except for the root node",
> 
> ---  
> Mark Volkmann

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [24 September 2007 20:01 UTC](https://rubytalk.org/t/getting-xpath-in-rexml-to-dive-deeper/40879/4 "2007-09-24T20:01:00Z")

</div>

Gavin Kistner wrote:

> Peter Bailey wrote:
> 
> > I'm finally using REXML instead of plain vanilla Ruby to convert XML  
> > data. I've learned a bit about XPath and how REXML can use it. I've got  
> > a sample XML file whose root element is \<registration\>. Underneath that  
> > element are a bunch of children elements. So, when I do the instruction  
> > listed below, I get all of them. But, I don't get the last two elements  
> > that, to me anyway, are children, too. The only thing different about  
> > them is that they have children, too. Does anyone know the nomenclature  
> > for getting ALL of the children of an element, not just those that are  
> > immediate children. I guess I want grandkids, too!
> > 
> > XPath.each(doc, "//registration/\*") { |element| puts element.text }
> 
> The term you are looking for is 'descendants'. In XPath, '/' is  
> shorthand for the 'child' axis. You want the 'descendant' axis, which  
> has the shorthand (that you already used) of '//'.
> 
> So "//registration//\*" is "At any level, find an element named  
> 'registration', and then find every element below any of them."
> 
> You possible want "registration//\*" which is "every element in the  
> entire XML file at every level except for the root node", but that seems  
> rather pointless. (Why have a hierarchy in the XML if you're throwing  
> away the meaning?) Perhaps that will work for you, or perhaps you should  
> think further about exactly what elements you really want to traverse.
> 
> Hope that helps (oh, and this has nothing to do with Ruby, BTW :p)  
> Gavin

Yes, this helps. Thanks, Gavin. What I did, to get everything I needed  
was this:

XPath.each(doc, "//registration//\*") { |element| puts element.text }

I just put in a second "/" after registration. That picked up ALL of the  
descendants, and the grand-child elements, or, whatever they're called.  
My only problem is that the output is kind of spacey, with a lot more  
white space before those grand-child elements. -Peter

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
