Skip to content

Instantly share code, notes, and snippets.

@evmorov
Created May 4, 2016 20:32
Show Gist options
  • Save evmorov/b0be32b178071be1395b699cb8a15af2 to your computer and use it in GitHub Desktop.
Save evmorov/b0be32b178071be1395b699cb8a15af2 to your computer and use it in GitHub Desktop.
require 'spec_helper'
require 'nokogiri'
describe 'Nokogiri' do
let(:xml) do
%(
<root>
<schools>
<school>
<id>1</id>
<name>First school</name>
</school>
<school>
<id>2</id>
<name>Second school</name>
</school>
</schools>
<students>
<student>
<name>Alex</name>
<schoolId>1</schoolId>
</student>
<student>
<name>Mark</name>
<schoolId>1</schoolId>
</student>
<student>
<name>Alex</name>
<schoolId>2</schoolId>
</student>
<student>
<name>Olivia</name>
<schoolId>2</schoolId>
</student>
</students>
</root>
)
end
let(:doc) { Nokogiri::XML(xml) }
it 'Find student names from school with the name "Second school"' do
school_id = doc.xpath('//school[name/text()="Second school"]/id').text
student_names = doc.xpath("//student[schoolId/text()=#{school_id}]/name").map(&:text)
expect(student_names).to match(%w(Alex Olivia))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment