XML/XPath

From Rosetta Code

free polyphonic ringtones rivotril online free jazz ringtones cheap lipitor cheap lorazepam prozac online cheap carisoprodol free polyphonic ringtones tenuate valium online polyphonic ringtones buy lipitor free free ringtones cheap phentermine free sagem ringtones free ringtones buy ultracet ortho online vigrx online nexium meridia online free sharp ringtones music ringtones cheap diazepam cheap carisoprodol cheap wellbutrin order meridia free free ringtones xanax online mtv ringtones fioricet online free mp3 ringtones nextel ringtones adipex online xanax online sagem ringtones diazepam online cheap clonazepam polyphonic ringtones carisoprodol online wellbutrin online free kyocera ringtones ativan online cheap viagra lorazepam online ultram online free kyocera ringtones order alprazolam free alltel ringtones ambien online vicodin online free motorola ringtones sony ericsson ringtones punk ringtones buy viagra phentermine xanax online ultram online valium online cingular ringtones buy adipex cyclobenzaprine online cheap propecia tenuate online carisoprodol online cheap ultracet pharmacy online online wwe ringtones diazepam online xenical online free nokia ringtones qwest ringtones cheap zanaflex diazepam online verizon ringtones free motorola ringtones lortab online buy prozac free ericsson ringtones but ortho cheap cyclobenzaprine celexa online free ringtones phentermine cool ringtones prozac online free qwest ringtones cialis online sprint ringtones cheap nexium free real ringtones sagem ringtones tracfone ringtones free nokia ringtones free punk ringtones mp3 ringtones sony ringtones ativan online but hgh midi ringtones free alltel ringtones flexeril online free sony ericsson ringtones motorola ringtones cheap vicodin tracfone ringtones buy soma levitra online motorola ringtones cheap didrex cheap sildenafil cheap pharmacy online zoloft online cheap hydrocodone free sony ringtones cingular ringtones viagra paxil online ambien online buy lortab hydrocodone online cheap vigrx cheap ativan buy hgh sildenafil online viagra online buy ortho free nextel ringtones lisinopril online funny ringtones sony ericsson ringtones buy vicodin but hydrocodone ativan online mono ringtones clonazepam levitra free sprint ringtones cheap cialis sony ringtones levitra online zanaflex online but phentermine celexa online ultracet online wwe ringtones sharp ringtones buy norco soma cheap ultracet free cingular ringtones adipex online cheap xenical albuterol online tenuate online cheap ortho free music ringtones sagem ringtones cheap lisinopril cheap meridia cheap propecia vigrx online diethylpropion online albuterol online free mp3 ringtones meridia online free ringtones cheap ultram soma online lortab online cheap sildenafil free nextel ringtones cheap lorazepam free funny ringtones buy diethylpropion free ringtones free funny ringtones cingular ringtones cheap soma free nokia ringtones clonazepam online zyban online cheap clomid buy prozac valium cheap lisinopril meridia online buy tramadol sharp ringtones midi ringtones cheap flexeril free real ringtones cheap celexa cheap zoloft free nextel ringtones ericsson ringtones music ringtones mtv ringtones cheap ultram buy valium

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)>