# More information on REXML/XPath

**URL:** <https://www.ruby-forum.com/t/more-information-on-rexml-xpath/115456>\
**Category:** Ruby\
**Created:** [September 25, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/more-information-on-rexml-xpath/115456 "2007-09-25T14:15:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 25, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/more-information-on-rexml-xpath/115456/1 "2007-09-25T14:15:55Z")

</div>

Hello,  
I’m trying to convert xml files to SGML. The publishing system at our  
company uses SGML. And, basically, the SGML I want to create is just  
CALS, which is the SGML tables standard. So, I’m trying to use REXML to  
parse the XML and delineate all of the necessary elements that need to  
be converted. And, I need to do some gathering and sorting. My client,  
an editor in my company, wants to pick the first from the list of a  
series of elements and use that as text for a heading in the final  
document.  
Here’s a bit of the original. I’ve attached a sample XML file.  
  
…  
  
Energy/Nuclear  
Waste (hazardous/solid/interstate/nuclear)  
  
Here’s what I’m doing to just try and capture that first instance of  
, which, in this case, is “Energy/Nuclear.”  
codes = XPath.match( doc, “//registration/issueList/issue code[1]” )  
puts codes  
It’s giving me ALL of the s, no matter what I put there as a  
quantifier. I’ve tried “[1]”, as shown. I’ve tried [0], first,  
[position()\<2], but, the results all look the same. It just gives me the  
whole list, not the first entries I’m asking for.  
Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 25, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/more-information-on-rexml-xpath/115456/2 "2007-09-25T14:54:13Z")

</div>

Peter B. wrote:

> Hello,  
> I’m trying to convert xml files to SGML. The publishing system at our  
> company uses SGML. And, basically, the SGML I want to create is just  
> CALS, which is the SGML tables standard. So, I’m trying to use REXML to  
> parse the XML and delineate all of the necessary elements that need to  
> be converted. And, I need to do some gathering and sorting. My client,  
> an editor in my company, wants to pick the first from the list of a  
> series of elements and use that as text for a heading in the final  
> document.  
> Here’s a bit of the original. I’ve attached a sample XML file.  
>   
> …  
>   
> Energy/Nuclear  
> Waste (hazardous/solid/interstate/nuclear)  
>   
> Here’s what I’m doing to just try and capture that first instance of  
> , which, in this case, is “Energy/Nuclear.”  
> codes = XPath.match( doc, “//registration/issueList/issue code[1]” )  
> puts codes  
> It’s giving me ALL of the s, no matter what I put there as a  
> quantifier. I’ve tried “[1]”, as shown. I’ve tried [0], first,  
> [position()\<2], but, the results all look the same. It just gives me the  
> whole list, not the first entries I’m asking for.  
> Thanks,  
> Peter

Never mind. I think I got it. I’m obviously new to XML, so, I was  
confused about what’s an element and what’s an attribute. I did this and  
I’m getting closer:

codes = XPath.match( doc, “//registration/issueList/issue[1]/” )

Thanks, anyway.  
-Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 27, 2007, 4:39am UTC](https://www.ruby-forum.com/t/more-information-on-rexml-xpath/115456/3 "2007-09-27T04:39:12Z")

</div>

On 9/25/07, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > It’s giving me ALL of the s, no matter what I put there as a  
> > quantifier. I’ve tried “[1]”, as shown. I’ve tried [0], first,  
> > [position()\<2], but, the results all look the same. It just gives me the  
> > whole list, not the first entries I’m asking for.

You may also be interested in XPath.first():  
[http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/XPath.html#M002905](http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/XPath.html#M002905)

HTH,  
Keith
