Created
May 4, 2016 20:32
-
-
Save evmorov/b0be32b178071be1395b699cb8a15af2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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