XML/XPath: Difference between revisions

From Rosetta Code
< XML
Content added Content deleted
mNo edit summary
(revert spam)
Line 1: Line 1:
{{task}}
[http://ustall.org/web/nardi.htm nardi] [http://ustall.org/web/nvidia-geforce.htm nvidia geforce 6600 gt 256mb] [http://saibso.org/canon-powershot.htm canon powershot a95 plus] [http://ustall.org/web/bas-armagnac.htm bas armagnac] [http://davte.info/library/html/saitek.htm saitek 52] [http://helmed.info/topic/bacio-katia/ bacio katia carolina] [http://helmed.info/topic/aegolius/ aegolius] [http://davte.info/library/html/borse-dell.htm borse dell energia] [http://amohseni.info/img/styles/bwv/ bwv 182] [http://amohseni.info/img/styles/the-king/ the king of the golden hall] [http://nobinters.org/centro-per.htm centro per pulizia del viso] [http://saibso.org/prescott.htm prescott 478] [http://helmed.info/topic/samsung-tft/ samsung tft] [http://bloprofeldi.info/lib/navigatore-satellitare.htm navigatore satellitare gps] [http://amohseni.info/img/styles/pattinaggio-foto/ pattinaggio foto] [http://nobinters.org/royal-gigolos.htm royal gigolos california dreamin extend] [http://amohseni.info/img/styles/where-the/ where the children play] [http://nobinters.org/acer-travelmate.htm acer travelmate 4001] [http://psisemiya.com/images/small/webranking/ webranking] [http://psisemiya.com/images/small/www-lycos/ www lycos] [http://nobinters.org/portami-in.htm portami in cielo] [http://kinunia.cn/viajes-com.htm viajes com mx] [http://kinunia.cn/kenwood-chef.htm kenwood chef major robot da cucina] [http://davte.info/library/html/battletech.htm battletech] [http://kinunia.cn/epson-emptw.htm epson emp-tw20] [http://amohseni.info/img/styles/inger-nilsson/ inger nilsson] [http://saibso.org/kosova-mp.htm kosova mp3] [http://helmed.info/topic/mix-vibes/ mix vibes pro 4] [http://nobinters.org/bollon.htm bollon] [http://psisemiya.com/images/small/macchine-da/ macchine da caffe in cialde] [http://chaba.info/qtek-/ qtek - pda phone 2020] [http://saibso.org/brother-tn.htm brother tn8000] [http://amohseni.info/img/styles/pc-da/ pc da tavolo] [http://amohseni.info/img/styles/benoit-musica/ benoit musica] [http://kinunia.cn/telefoni-con.htm telefoni con filo] [http://helmed.info/topic/generale-di/ generale di de gregori] [http://bloprofeldi.info/lib/antrozous.htm antrozous] [http://chaba.info/accessori-gps/ accessori gps] [http://davte.info/library/html/alimento-cane.htm alimento cane] [http://kinunia.cn/sachsen.htm sachsen] [http://amohseni.info/img/styles/link-esibizionisti/ link esibizionisti] [http://kinunia.cn/diffusori-e.htm diffusori e casse] [http://saibso.org/facce-di.htm facce di clown foto] [http://kinunia.cn/acer-aspire.htm acer aspire 5024wlmi turion ml34 512mb] [http://bloprofeldi.info/lib/if-god.htm if god will send his angels] [http://ashythro.info/library/html/g-mazzini/ g mazzini giovine italia] [http://ustall.org/web/cavalie.htm cavalie] [http://bloprofeldi.info/lib/hamster-ball.htm hamster ball] [http://psisemiya.com/images/small/robbie-williams/ robbie williams. nobody someday] [http://davte.info/library/html/la-bayadere.htm la bayadere] [http://helmed.info/topic/high-grow/ high grow] [http://kinunia.cn/sms-logo.htm sms logo nec e606] [http://ustall.org/web/manu.htm manu] [http://ustall.org/web/monitor-lg.htm monitor lg 19 lcd] [http://bloprofeldi.info/lib/il-processo.htm il processo del secolo] [http://psisemiya.com/images/small/oakley-iridium/ oakley iridium] [http://kinunia.cn/monitor-benq.htm monitor benq lcd 19 fp931] [http://davte.info/library/html/soledad.htm soledad] [http://kinunia.cn/forster-john.htm forster, john] [http://nobinters.org/lultima-pattuglia.htm lultima pattuglia] [http://davte.info/library/html/offerte-lavoro.htm offerte lavoro infermieri svizzera] [http://kinunia.cn/most.htm most] [http://saibso.org/oh-happy.htm oh happy days sister act] [http://saibso.org/mobiline.htm mobiline] [http://saibso.org/i-nostri.htm i nostri sogni] [http://psisemiya.com/images/small/on-my/ on my knees 411] [http://nobinters.org/sapphire-x.htm sapphire x550 256mb] [http://amohseni.info/img/styles/i-want/ i want to miss a thing] [http://davte.info/library/html/breath-midge.htm breath midge ure] [http://helmed.info/topic/iposte/ iposte] [http://nobinters.org/www-formaper.htm www formaper com] [http://nobinters.org/formulario-giuridico.htm formulario giuridico] [http://nobinters.org/lo-scandalo.htm lo scandalo del vestito bianco] [http://ustall.org/web/boow.htm boow] [http://psisemiya.com/images/small/calma-e/ calma e sangu freddo] [http://ashythro.info/library/html/anello-elica/ anello elica] [http://saibso.org/creazione-sito.htm creazione sito internet albenga] [http://davte.info/library/html/protezione-lcd.htm protezione lcd] [http://psisemiya.com/images/small/castelrotto-val/ castelrotto val gardena] [http://bloprofeldi.info/lib/offertorio.htm offertorio] [http://psisemiya.com/images/small/finanziamento-lucca/ finanziamento lucca] [http://saibso.org/volo-montreal.htm volo montreal] [http://kinunia.cn/immagini-ragazze.htm immagini ragazze nude gratis] [http://bloprofeldi.info/lib/dj-di.htm dj di musica electronica in italia] [http://ustall.org/web/stupid-girl.htm stupid girl] [http://chaba.info/voss-johann/ voss, johann heinrich] [http://chaba.info/monitor-lcd/ monitor lcd tuner] [http://kinunia.cn/diesel-camicie.htm diesel camicie donna] [http://davte.info/library/html/albergo-cortina.htm albergo cortina d ampezzo] [http://nobinters.org/barabba.htm barabba] [http://ustall.org/web/cheap-and.htm cheap and chic] [http://chaba.info/sparkle-gf/ sparkle gf6600 256mb pci-express] [http://psisemiya.com/images/small/una-corsa/ una corsa sul prato] [http://ustall.org/web/merz-backstage.htm merz backstage] [http://davte.info/library/html/gps-blue.htm gps blue tooth nemerix] [http://chaba.info/schede-video/ schede video per pc] [http://amohseni.info/img/styles/divinacommedia/ divinacommedia] [http://nobinters.org/lettore-dvd.htm lettore dvd divx dv-380-s] [http://saibso.org/yuiop.htm yuiop] [http://nobinters.org/animaciones-flash.htm animaciones flash] [http://bloprofeldi.info/lib/forio-ischia.htm forio ischia] [http://kinunia.cn/o-zone.htm o zone mai ia hii] [http://kinunia.cn/lombra-della.htm lombra della vendetta] [http://kinunia.cn/c-olympus.htm c8080 olympus] [http://nobinters.org/telefonino-samsung.htm telefonino samsung z300] [http://nobinters.org/mp.htm mp3 1] [http://kinunia.cn/u-got.htm u got your self] [http://davte.info/library/html/fairfield-australia.htm fairfield (australia)] [http://davte.info/library/html/hotel-villamarina.htm hotel villamarina di cesenatico] [http://amohseni.info/img/styles/kenwood-w/ kenwood w6531] [http://ustall.org/web/capareza.htm capareza] [http://psisemiya.com/images/small/rondanina/ rondanina] [http://ashythro.info/library/html/donna-reed/ donna reed] [http://nobinters.org/nuove-immagini.htm nuove immagini senza preavviso] [http://kinunia.cn/buddha-bar.htm buddha bar vi] [http://ustall.org/web/rayban-occhiali.htm rayban occhiali] [http://saibso.org/poetare.htm poetare] [http://saibso.org/behringer-kt.htm behringer kt108] [http://chaba.info/paragonite/ paragonite] [http://psisemiya.com/images/small/settimana-bianca/ settimana bianca a pila] [http://helmed.info/topic/baloon-/ baloon 5 traduzione] [http://amohseni.info/img/styles/is-it/ is it cos im cool] [http://davte.info/library/html/jvc-ths.htm jvc th-s1] [http://psisemiya.com/images/small/box-esterno/ box esterno 3 5 usb ata] [http://davte.info/library/html/manubrio-kg.htm manubrio kg] [http://helmed.info/topic/vacanze-ottobre/ vacanze ottobre] [http://nobinters.org/stacy-fergusson.htm stacy fergusson] [http://davte.info/library/html/philips-impianti.htm philips impianti hi-fi] [http://bloprofeldi.info/lib/velsen.htm velsen] [http://helmed.info/topic/zephir-condizionatore/ zephir condizionatore] [http://saibso.org/casamicciola.htm casamicciola] [http://davte.info/library/html/fan-fiction.htm fan fiction site] [http://chaba.info/sermon/ sermon] [http://bloprofeldi.info/lib/barbara-durso.htm barbara durso] [http://chaba.info/locali-hard/ locali hard] [http://ashythro.info/library/html/trittico-damore/ trittico damore] [http://ashythro.info/library/html/kodak-/ kodak - easyshare cx7330] [http://ustall.org/web/ups.htm ups 650] [http://davte.info/library/html/macchine-caffe.htm macchine caffe krups] [http://psisemiya.com/images/small/losing-my/ losing my] [http://chaba.info/tiziana-sensi/ tiziana sensi] [http://ustall.org/web/fuji-.htm fuji - finepix s5500] [http://kinunia.cn/jennifer-connelly.htm jennifer connelly nuda] [http://saibso.org/fort-washington.htm fort washington - vita da cani] [http://amohseni.info/img/styles/scarpe-lizard/ scarpe lizard] [http://bloprofeldi.info/lib/time-to.htm time to rock] [http://kinunia.cn/la-mano.htm la mano che uccide] [http://helmed.info/topic/porsche/ porsche 2003 996] [http://chaba.info/franchising-abbronzatura/ franchising abbronzatura] [http://amohseni.info/img/styles/vaio-/ vaio 17 notebook] [http://chaba.info/auto-noleggio/ auto noleggio benevento] [http://kinunia.cn/frigoriferi-incasso.htm frigoriferi incasso 320] [http://davte.info/library/html/ituiutaba.htm ituiutaba] [http://bloprofeldi.info/lib/set-posate.htm set posate] [http://chaba.info/con-origen/ con origen nel deserto della california] [http://nobinters.org/gay-e.htm gay e transex] [http://amohseni.info/img/styles/stefania-lay/ stefania lay] [http://helmed.info/topic/curriculum-vitae/ curriculum vitae europeo] [http://ashythro.info/library/html/un-medico/ un medico in famiglia. prima serie. vol. 01] [http://amohseni.info/img/styles/www-roten/ www roten com] [http://bloprofeldi.info/lib/monitor-yakumo.htm monitor yakumo 17 lcd] [http://helmed.info/topic/seit-elettronica/ seit elettronica srl] [http://nobinters.org/hard-disck.htm hard disck 80gb] [http://chaba.info/fiat-punto/ fiat punto 1.2 16v sporting] [http://chaba.info/canon-cblue/ canon cb-2lue] [http://amohseni.info/img/styles/godwin-austen/ godwin austen] [http://nobinters.org/motoraduni-.htm motoraduni 2004 vicenza] [http://nobinters.org/emma-shapplin.htm emma shapplin - carmine meo] [http://bloprofeldi.info/lib/danu.htm danu] [http://bloprofeldi.info/lib/righeira-karaoke.htm righeira karaoke] [http://amohseni.info/img/styles/calciocom/ calcio.com] [http://chaba.info/konica-minolta/ konica minolta dimage 600] [http://kinunia.cn/rebel-a.htm rebel a k a] [http://bloprofeldi.info/lib/forno-con.htm forno con piano cottura] [http://saibso.org/clipart-cobra.htm clipart cobra] [http://saibso.org/videochatgratis.htm videochatgratis] [http://bloprofeldi.info/lib/accessori-per.htm accessori per interni audi 4] [http://bloprofeldi.info/lib/miratoio.htm miratoio] [http://amohseni.info/img/styles/mac-os/ mac os 10 4 tiger] [http://chaba.info/sacrifice-by/ sacrifice by lisa gerard and peter burke] [http://ustall.org/web/von-savigny.htm von savigny] [http://bloprofeldi.info/lib/udo-samel.htm udo samel] [http://psisemiya.com/images/small/jamie-walters/ jamie walters songs] [http://ustall.org/web/top-sensation.htm top sensation] [http://ustall.org/web/dulce-maria.htm dulce maria clase 406] [http://saibso.org/posto-al.htm posto al sole rai it] [http://davte.info/library/html/bella-balilla.htm bella balilla] [http://chaba.info/platinum-wall/ platinum wall] [http://bloprofeldi.info/lib/dating-sites.htm dating sites] [http://kinunia.cn/tu-che.htm tu che] [http://davte.info/library/html/foto-arcuri.htm foto arcuri] [http://nobinters.org/totali-.htm totali spies] [http://kinunia.cn/frigorifero-no.htm frigorifero no frost classe a silver] [http://bloprofeldi.info/lib/miguel-bose.htm miguel bose super superman] [http://saibso.org/omicidio-per.htm omicidio per vocazione] [http://davte.info/library/html/minueto.htm minueto] [http://helmed.info/topic/la-celiachia/ la celiachia] [http://amohseni.info/img/styles/notebook-schede/ notebook schede acquisizione e tv] [http://nobinters.org/lamiere-reggio.htm lamiere reggio emilia] [http://bloprofeldi.info/lib/rx-v.htm rx v2500] {{task}}


Perform the following three XPath queries on the XML Document below:
Perform the following three XPath queries on the XML Document below:
Line 87: Line 87:
</cfsavecontent>
</cfsavecontent>
<cfset xml = xmlParse(xmlString)>
<cfset xml = xmlParse(xmlString)>
&lt;!--- First Task --->
<cfset itemSearch = xmlSearch(xml, "//item")>
&lt;!--- item = the first Item (xml element object) --->
<cfset item = itemSearch[1]>
&lt;!--- Second Task --->
<cfset priceSearch = xmlSearch(xml, "//price")>
&lt;!--- loop and print each price --->
<cfloop from="1" to="#arrayLen(priceSearch)#" index="i">
#priceSearch[i].xmlText#&lt;br/>
</cfloop>
&lt;!--- Third Task --->
&lt;!--- array of all the name elements --->
<cfset names = xmlSearch(xml, "//name")>
&lt;!--- visualize the results --->
<cfdump var="#variables#">

==[[JavaScript]]==
[[Category:JavaScript]]

'''Interpreter:''' Firefox 2.0

//create XMLDocument object from file
var xhr = new XMLHttpRequest();
xhr.open('GET', 'file.xml', false);
xhr.send(null);
var doc = xhr.responseXML;
//get first <item> element
var firstItem = doc.evaluate( '//item[1]', doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
alert( firstItem.textContent );
//output contents of <price> elements
var prices = doc.evaluate( '//price', doc, null, XPathResult.ANY_TYPE, null );
for( var price = prices.iterateNext(); price != null; price = prices.iterateNext() ) {
alert( price.textContent );
}
//add <name> elements to array
var names = doc.evaluate( '//name', doc, null, XPathResult.ANY_TYPE, null);
var namesArray = [];
for( var name = names.iterateNext(); name != null; name = names.iterateNext() ) {
namesArray.push( name );
}
alert( namesArray );

Although some browsers support XPath, working with XML is much easier with E4X.

//create XML object from file
var xhr = new XMLHttpRequest();
xhr.open('GET', 'file.xml', false);
xhr.send(null);
var doc = new XML(xhr.responseText);
//get first <item> element
var firstItem = doc..item[0];
alert( firstItem );
//output contents of <price> elements
for each( var price in doc..price ) {
alert( price );
}
//add <name> elements to array
var names = [];
for each( var name in doc..name ) {
names.push( name );
}
alert( names );

==[[Perl]]==
[[Category:Perl]]
use XML::XPath qw();
my $x = XML::XPath->new('<inventory ... </inventory>');
[$x->findnodes('//item[1]')->get_nodelist]->[0];
print $x->findnodes_as_string('//price');
$x->findnodes('//name')->get_nodelist;

==[[PHP]]==
[[Category:PHP]]
<?php
//PHP5 only example due to changes in XML extensions between version 4 and 5 (Tested on PHP5.2.0)
$doc = DOMDocument::loadXML('<inventory title="OmniCorp Store #45x10^3">...</inventory>');
//Load from file instead with $doc = DOMDocument::load('filename');
$xpath = new DOMXPath($doc);
/*
1st Task: Retrieve the first "item" element
*/
$nodelist = $xpath->query('//item');
$result = $nodelist->item(0);
/*
2nd task: Perform an action on each "price" element (print it out)
*/
$nodelist = $xpath->query('//price');
for($i = 0; $i < $nodelist->length; $i++)
{
//print each price element in the DOMNodeList instance, $nodelist, as text/xml followed by a newline
print $doc->saveXML($nodelist->item($i))."\n";
}
/*
3rd Task: Get an array of all the "name" elements
*/
$nodelist = $xpath->query('//name');
//our array to hold all the name elements, though in practice you'd probably not need to do this and simply use the DOMNodeList
$result = array();
//a different way of iterating through the DOMNodeList
foreach($nodelist as $node)
{
$result[] = $node;
}

==[[Ruby]]==
[[Category:Ruby]]
#Example taken from the REXML tutorial (http://www.germane-software.com/software/rexml/docs/tutorial.html)
require "rexml/document"
include REXML
#create the REXML Document from the string (%q is Ruby's multiline string, everything between the two @-characters is the string)
doc = Document.new(
%q@<inventory title="OmniCorp Store #45x10^3">
...
</inventory>
@
)
# The invisibility cream is the first <item>
invisibility = XPath.first( doc, "//item" )
# Prints out all of the prices
XPath.each( doc, "//price") { |element| puts element.text }
# Gets an array of all of the "name" elements in the document.
names = XPath.match( doc, "//name" )

Revision as of 01:18, 18 August 2007

Task
XML/XPath
You are encouraged to solve this task according to the task description, using any language you may know.

Perform the following three XPath queries on the XML Document below:

  • Retrieve the first "item" element
  • Perform an action on each "price" element (print it out)
  • Get an array of all the "name" elements

XML Document:

<inventory title="OmniCorp Store #45x10^3">
  
<item upc="123456789" stock="12"> <name>Invisibility Cream</name> <price>14.50</price> <description>Makes you invisible</description> </item> <item upc="445322344" stock="18"> <name>Levitation Salve</name> <price>23.99</price> <description>Levitate yourself for up to 3 hours per application</description> </item>
<item upc="485672034" stock="653"> <name>Blork and Freen Instameal</name> <price>4.95</price> <description>A tasty meal in a tablet; just add water</description> </item> <item upc="132957764" stock="44"> <name>Grob winglets</name> <price>3.56</price> <description>Tender winglets of Grob. Just add water</description> </item>
</inventory>

C#

 XmlReader XReader;

 // Either read the xml from a string ...
 XReader = XmlReader.Create(new StringReader("<inventory title=... </inventory>"));

 // ... or read it from the file system.
 XReader = XmlReader.Create("xmlfile.xml");

 // Create a XPathDocument object (which implements the IXPathNavigable interface)
 // which is optimized for XPath operation. (very fast).
 IXPathNavigable XDocument = new XPathDocument(XReader);

 // Create a Navigator to navigate through the document.
 XPathNavigator Nav = XDocument.CreateNavigator();
 Nav = Nav.SelectSingleNode("//item");

 // Move to the first element of the selection. (if available).
 if(Nav.MoveToFirst())
 {
   Console.WriteLine(Nav.OuterXml); // The outer xml of the first item element.
 }

 // Get an iterator to loop over multiple selected nodes.
 XPathNodeIterator Iterator = XDocument.CreateNavigator().Select("//price");

 while (Iterator.MoveNext())
 {
   Console.WriteLine(Iterator.Current.Value);
 }

 Iterator = XDocument.CreateNavigator().Select("//name");

 // Use a generic list.
 List<string> NodesValues = new List<string>();

 while (Iterator.MoveNext())
 {
   NodesValues.Add(Iterator.Current.Value);
 }

 // Convert the generic list to an array and output the count of items.
 Console.WriteLine(NodesValues.ToArray().Length);

ColdFusion

 <cfsavecontent variable="xmlString">
 <inventory
 ...
 </inventory>
 </cfsavecontent>
 <cfset xml = xmlParse(xmlString)>
 <!--- First Task --->
 <cfset itemSearch = xmlSearch(xml, "//item")>
 <!--- item = the first Item (xml element object) --->
 <cfset item = itemSearch[1]>
 <!--- Second Task --->
 <cfset priceSearch = xmlSearch(xml, "//price")>
 <!--- loop and print each price --->
 <cfloop from="1" to="#arrayLen(priceSearch)#" index="i">
   #priceSearch[i].xmlText#<br/>
 </cfloop>
 <!--- Third Task --->
 <!--- array of all the name elements --->
 <cfset names = xmlSearch(xml, "//name")>
 <!--- visualize the results --->
 <cfdump var="#variables#">

JavaScript

Interpreter: Firefox 2.0

//create XMLDocument object from file
var xhr = new XMLHttpRequest();
xhr.open('GET', 'file.xml', false);
xhr.send(null);
var doc = xhr.responseXML;

//get first <item> element
var firstItem = doc.evaluate( '//item[1]', doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
alert( firstItem.textContent );

//output contents of <price> elements
var prices = doc.evaluate( '//price', doc, null, XPathResult.ANY_TYPE, null );
for( var price = prices.iterateNext(); price != null; price = prices.iterateNext() ) {
  alert( price.textContent );
}

//add <name> elements to array
var names = doc.evaluate( '//name', doc, null, XPathResult.ANY_TYPE, null);
var namesArray = [];
for( var name = names.iterateNext(); name != null; name = names.iterateNext() ) {
  namesArray.push( name );
}
alert( namesArray );

Although some browsers support XPath, working with XML is much easier with E4X.

//create XML object from file
var xhr = new XMLHttpRequest();
xhr.open('GET', 'file.xml', false);
xhr.send(null);
var doc = new XML(xhr.responseText);

//get first <item> element
var firstItem = doc..item[0];
alert( firstItem );

//output contents of <price> elements
for each( var price in doc..price ) {
  alert( price );
}

//add <name> elements to array
var names = [];
for each( var name in doc..name ) {
  names.push( name );
}
alert( names );

Perl

use XML::XPath qw();
my $x = XML::XPath->new('<inventory ... </inventory>');

[$x->findnodes('//item[1]')->get_nodelist]->[0];
print $x->findnodes_as_string('//price');
$x->findnodes('//name')->get_nodelist;

PHP

 <?php
 //PHP5 only example due to changes in XML extensions between version 4 and 5 (Tested on PHP5.2.0)
 $doc = DOMDocument::loadXML('<inventory title="OmniCorp Store #45x10^3">...</inventory>');
 //Load from file instead with $doc = DOMDocument::load('filename');
 $xpath = new DOMXPath($doc);
 /* 
     1st Task: Retrieve the first "item" element
 */
 $nodelist = $xpath->query('//item');
 $result = $nodelist->item(0);
 /* 
     2nd task: Perform an action on each "price" element (print it out)
 */
 $nodelist = $xpath->query('//price');
 for($i = 0; $i < $nodelist->length; $i++)
 {
   //print each price element in the DOMNodeList instance, $nodelist, as text/xml followed by a newline
   print $doc->saveXML($nodelist->item($i))."\n";
 }
 /* 
     3rd Task: Get an array of all the "name" elements
 */
 $nodelist = $xpath->query('//name');
 //our array to hold all the name elements, though in practice you'd probably not need to do this and simply use the DOMNodeList
 $result = array(); 
 //a different way of iterating through the DOMNodeList
 foreach($nodelist as $node)
 {
   $result[] = $node; 
 }

Ruby

 #Example taken from the REXML tutorial (http://www.germane-software.com/software/rexml/docs/tutorial.html)
 require "rexml/document"
 include REXML
 #create the REXML Document from the string (%q is Ruby's multiline string, everything between the two @-characters is the string)
 doc = Document.new(
         %q@<inventory title="OmniCorp Store #45x10^3">
              ...
            </inventory>
           @
                           )
 # The invisibility cream is the first <item>
 invisibility = XPath.first( doc, "//item" ) 
 # Prints out all of the prices
 XPath.each( doc, "//price") { |element| puts element.text }
 # Gets an array of all of the "name" elements in the document.
 names = XPath.match( doc, "//name" )