XML/XPath: Difference between revisions

(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 2,649:
print $x->findnodes_as_string('//price');
$x->findnodes('//name')->get_nodelist;</lang>
 
=={{header|Phix}}==
Phix has no direct support for XPath, but achieving the requirements using the standard xml_parse() is not exactly difficult.
<lang Phix>include builtins/xml.e
constant xml_txt = """
<inventory title="OmniCorp Store #45x10^3">
<section name="health">
<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>
</section>
<section name="food">
<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>
</section>
</inventory>
""",
-- or, of course, xml_txt = get_text("input.xml")
xml = xml_parse(xml_txt)
 
sequence sections = xml_get_nodes(xml[XML_CONTENTS],"section"),
item1 = {},
prices = {},
names = {}
 
for s=1 to length(sections) do
sequence items = xml_get_nodes(sections[s],"item")
if item1={} then item1 = items[1] end if
for i=1 to length(items) do
prices = append(prices,xml_get_nodes(items[i],"price")[1][XML_CONTENTS])
names = append(names,xml_get_nodes(items[i],"name")[1][XML_CONTENTS])
end for
end for
 
puts(1,"===item[1]===\n")
sequence tmp = xml_new_doc(item1)
puts(1,xml_sprint(tmp))
puts(1,"===prices===\n")
pp(prices)
puts(1,"===names===\n")
pp(names,{pp_Maxlen,90})</lang>
{{out}}
<pre>
===item[1]===
<?xml version="1.0" encoding="utf-8" ?>
<item upc="123456789" stock="12">
<name>Invisibility Cream</name>
<price>14.50</price>
<description>Makes you invisible</description>
</item>
===prices===
{`14.50`, `23.99`, `4.95`, `3.56`}
===names===
{`Invisibility Cream`, `Levitation Salve`, `Blork and Freen Instameal`, `Grob winglets`}
</pre>
 
=={{header|PHP}}==
7,794

edits