Limiting the number of XML records returned

require 'rexml/document'
include REXML

xml =<<XML
<root>
  <item>2</item>
  <item>4</item>
  <item>6</item>
  <item>8</item>
  <item>10</item>
  <item>12</item>
  <item>14</item>
</root>
XML

doc = Document.new(xml)
node = XPath.match(doc.root,'item[position() > 2 and position() < 5]')
puts node

Output:

<item>6</item>
<item>8</item>

Resources