Anonymous user
XML/XPath: Difference between revisions
m
Fixed lang tags.
(added Haskell (without using specialized modules)) |
m (Fixed lang tags.) |
||
Line 35:
=={{header|AutoHotkey}}==
With regular expressions
<lang AutoHotkey>FileRead, inventory, xmlfile.xml
RegExMatch(inventory, "<item.*?</item>", item1)
Line 47 ⟶ 46:
While, pos := RegExMatch(inventory, "<name>.*?</name>", name, pos + 1)
names .= name . "`n"
MsgBox % names</lang>
{{libheader|AHK XPath}}
<lang AutoHotkey>#Include xpath.ahk
xpath_load(doc, "xmlfile.xml")
Line 65 ⟶ 62:
; Get an array of all the "name" elements
MsgBox % xpath(doc, "/inventory/section/item/name")</lang>
=={{header|C sharp|C#}}==
<lang csharp>
=={{header|C++}}==
<lang
#include <string>
#include <iostream>
Line 174 ⟶ 170:
std::copy( names.begin( ) , names.end( ) , std::ostream_iterator<std::string>( std::cout , "\n" )) ;
return 0 ;
}</lang>
=={{header|ColdFusion}}==
<lang
=={{header|D}}==
It is important to note that the KXML library currently only supports XPath minimally.
{{libheader|KXML}}
<lang d>import kxml.xml;
char[]xmlinput =
"<inventory title=\"OmniCorp Store #45x10^3\">
Line 240 ⟶ 234:
}
auto namearray = root.parseXPath("inventory/section/item/name");
}</lang>
=={{header|Groovy}}==
Line 279 ⟶ 271:
=={{header|JavaScript}}==
{{works with|Firefox|2.0}}
<lang javascript>
Although some browsers support XPath, working with XML is much easier with E4X.
<lang javascript>
=={{header|Perl}}==
{{libheader|XML::XPath}}
<lang perl>
=={{header|PHP}}==
<lang 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::
$xpath = new DOMXPath($doc);
/*
1st Task: Retrieve the first "item" element
*/
$nodelist = $xpath->query('//item');
/*
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;
}</lang>
=={{header|Python}}==
<lang python># Python has basic xml parsing built in
from xml.dom import minidom
Line 390 ⟶ 379:
# 3rd Task: Get an array of all the "name" elements
namesArray = xmldoc.getElementsByTagName("name")</lang>
=={{header|R}}==
{{libheader|XML (R)}}
<lang R>## Require the XML package you can download from http://www.omegahat.org/RSXML/
library("XML")
doc <- xmlInternalTreeParse("test3.xml")
Line 405 ⟶ 392:
for(i in 1:length(prices)) print(prices[i])
# 3rd Task: Get an array of all the "name" elements
(namesArray <- sapply(getNodeSet(doc, "//name"), xmlValue))</lang>
=={{header|Ruby}}==
{{libheader|REXML}}
<lang ruby>
=={{header|Scala}}==
Line 436 ⟶ 421:
better show the results.
<lang scala>scala> val xml = <inventory title="OmniCorp Store #45x10^3">
| <section name="health">
| <item upc="123456789" stock="12">
Line 516 ⟶ 500:
scala> val values = xml \\ "name" map (_ text) toArray
values: Array[String] = Array(Invisibility Cream, Levitation Salve, Blork and Freen Instameal, Grob winglets)</lang>
=={{header|Tcl}}==
Line 538 ⟶ 521:
=={{header|Visual Basic .NET}}==
<lang
=={{header|XQuery}}==
<lang xquery>(:
1. Retrieve the first "item" element
Notice the braces around //item. This evaluates first all item elements and then retrieving the first one.
Line 569 ⟶ 550:
<prices>{$price}</prices>
<names>{$names}</names>
</result></lang>
Performing this XQuery on the given input document results in
<lang xquery><?xml version="1.0" encoding="UTF-8"?>
<result>
<firstItem>
Line 590 ⟶ 569:
<name>Grob winglets</name>
</names>
</result></lang>
=={{header|XSLT}}==
<lang xml><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
Line 619 ⟶ 595:
<xsl:copy-of select="//name" />
</xsl:template>
</xsl:stylesheet></lang>
|