XML/XPath

From Rosetta Code
< XML
Revision as of 11:37, 10 September 2007 by rosettacode>KmgBi2

www diddle barzellette ufficio yulia mayarchuck nuda zoccolone www ausoniatools com berlino esto bumer klingelton al piccolo margherita www basketball it barbiere della sera yates britney foto www lunapop com basic instict busco familia brigante si more weisse w w w melamara rocks it www lisola it baila la negra www sunia it blek and pics xing bandier rossa benedetto sei tu www infocamere it zstar britany spears nuda www unt se aspetta primavera bandini brono martino www sussidiario it yo yo bella jovanotti youtouchmytralala yammat braccialetti agente federale x 3 barbara durso wverytime www zzchat com www fotos de culos orq www chistes balao magico britney spears tutto amore gitano beetlejuice brian kraus nudo bobbi sue luther www itullians com biblia associazione di cultura biblica zavidovici bon jovi zozzone biglietti supercoppa basic instinct 2 wolsvagen www taxus it xcam dvb www gesu liberatore it www soluzioniecostruzioni com wwwloredanabontempi it ylli trokites zeze di camargo midi belle in perizoma www ss ortana calcio it brigitta kerkova bacididonna boule di frutta xvid zostawcie ww alitalia it bur regione calabria zanussiprofessional it yimou www scozia com benedetti com yaga mackie ranks clase aparte zigano balla balla ballerino www ostaggi usa com zain www thefamilyitalia it bando di concorso corpo forestale blue gulity zuccherina www happidea it www travis it boys dziewczonka www tittypalace it www sexape com barry white testi gratis black eied peace www sulpm it www radio101 it brik brak napoli benedetta passione www infostrada spazio zero it yeha bacher boy 2 yooom wara yooom xin tai ruan yannick xpie box auto lamiera borsalavoro bilder irak www torremolinos com bruno del turco zaza briggetta bui bonnie tayler boiardo vita e opere allarme a gibilterra www fantasmas co bacisaffici a pugni nudi yoli babasonicos www vicenza com www raitre rai it y ahoo com www uff com mx www mans com attenzione alla puttana santa www 3sudest ro www evatremila bullismo angeli del peccato bob dylan knock on heavens door baile del pescao zoo 105 yogurth brujerias agenti speciali onu missione eiffel www lippa it zicconi www alloggio it bachatas del grupo aventura www corsera www spartiti it brooke berry blue guilty bublin brigitte kerkova amore selvaggio www peepeegirls it bruges vacanze zaini nike www tgpu com www goggi d alessio it www jimi hendrix iut

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.W

2000 riteLine(NodesValues.ToArray().Length);

ColdFusion

 <cfsavecontent variable="xmlString">
 <inventory
 ...
 </inventory>
 </cfsavecontent>
 <cfset xml = xmlParse(xmlString)>