From 3969b5ef51491fa05f6b9219f09c99bc61dd6379 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sat, 21 Mar 2015 01:36:10 +0100 Subject: [PATCH] Expanded XPath/CSS examples in the README Fixes #83 --- README.md | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c966abb..32087a0 100644 --- a/README.md +++ b/README.md @@ -76,15 +76,45 @@ Parse a string of XML using the SAX parser: Querying a document using XPath: - document = Oga.parse_xml('Alice') + document = Oga.parse_xml <<-EOF + + + Alice + 28 + + + EOF - document.xpath('string(people/person)') # => "Alice" + # The "xpath" method returns an enumerable (Oga::XML::NodeSet) that you can + # iterate over. + document.xpath('people/person').each do |person| + puts person.get('id') # => "1" -Querying a document using CSS: + # The "at_xpath" method returns a single node from a set, it's the same as + # person.xpath('name').first. + puts person.at_xpath('name').text # => "Alice" + end - document = Oga.parse_xml('Alice') +Querying the same document using CSS: - document.css('people person') # => NodeSet(Element(name: "person" ...)) + document = Oga.parse_xml <<-EOF + + + Alice + 28 + + + EOF + + # The "css" method returns an enumerable (Oga::XML::NodeSet) that you can + # iterate over. + document.css('people person').each do |person| + puts person.get('id') # => "1" + + # The "at_css" method returns a single node from a set, it's the same as + # person.css('name').first. + puts person.at_css('name').text # => "Alice" + end Modifying a document and serializing it back to XML: