Jump to content

XML/XPath: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|C++}}: Replaced broken example with one that actually uses XPath.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 33:
</section>
</inventory>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
Line 277 ⟶ 278:
 
</pre>
 
=={{header|AppleScript}}==
'''Using System Events'''
 
''AppleScript has no-built in support for XPath, but it could be used via a 'do shell script' command. Here's a solution using Apple System Events.
 
<lang AppleScript>set theXMLdata to "<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>"
 
on getElementValuesByName(theXML, theNameToFind)
set R to {}
tell application "System Events"
repeat with i in theXML
set {theName, theElements} to {i's name, i's XML elements}
if (count of theElements) > 0 then set R to R & my getElementValuesByName(theElements, theNameToFind)
if theName = theNameToFind then set R to R & i's value
end repeat
end tell
return R
end getElementValuesByName
 
on getBlock(theXML, theItem, theInstance)
set text item delimiters to ""
repeat with i from 1 to theInstance
set {R, blockStart, blockEnd} to {{}, "<" & theItem & space, "</" & theItem & ">"}
set x to offset of blockStart in theXML
if x = 0 then exit repeat
set y to offset of blockEnd in (characters x thru -1 of theXML as string)
if y = 0 then exit repeat
set R to characters x thru (x + y + (length of blockEnd) - 2) of theXML as string
set theXML to characters (y + (length of blockEnd)) thru -1 of theXML as string
end repeat
return R
end getBlock
 
tell application "System Events"
set xmlData to make new XML data with properties {name:"xmldata", text:theXMLdata}
return my getBlock(xmlData's text, "item", 1) -- Solution to part 1 of problem.
return my getElementValuesByName(xmlData's contents, "name") -- Solution to part 2 of problem.
return my getElementValuesByName(xmlData's contents, "price") -- Solution to part 3 of problem.
end tell</lang>Output for the three results (respectively):<lang AppleScript>"<item upc=\"123456789\" stock=\"12\">
<name>Invisibility Cream</name>
<price>14.50</price>
<description>Makes you invisible</description>
</item>"
 
{"Invisibility Cream", "Levitation Salve", "Blork and Freen Instameal", "Grob winglets"}
 
{"14.50", "23.99", "4.95", "3.56"}</lang>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
Line 537 ⟶ 614:
 
</pre>
 
=={{header|AutoHotkey}}==
With regular expressions
Line 567 ⟶ 645:
; Get an array of all the "name" elements
MsgBox % xpath(doc, "/inventory/section/item/name")</lang>
 
 
=={{header|AppleScript}}==
'''Using System Events'''
 
''AppleScript has no-built in support for XPath, but it could be used via a 'do shell script' command. Here's a solution using Apple System Events.
 
<lang AppleScript>set theXMLdata to "<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>"
 
on getElementValuesByName(theXML, theNameToFind)
set R to {}
tell application "System Events"
repeat with i in theXML
set {theName, theElements} to {i's name, i's XML elements}
if (count of theElements) > 0 then set R to R & my getElementValuesByName(theElements, theNameToFind)
if theName = theNameToFind then set R to R & i's value
end repeat
end tell
return R
end getElementValuesByName
 
on getBlock(theXML, theItem, theInstance)
set text item delimiters to ""
repeat with i from 1 to theInstance
set {R, blockStart, blockEnd} to {{}, "<" & theItem & space, "</" & theItem & ">"}
set x to offset of blockStart in theXML
if x = 0 then exit repeat
set y to offset of blockEnd in (characters x thru -1 of theXML as string)
if y = 0 then exit repeat
set R to characters x thru (x + y + (length of blockEnd) - 2) of theXML as string
set theXML to characters (y + (length of blockEnd)) thru -1 of theXML as string
end repeat
return R
end getBlock
 
tell application "System Events"
set xmlData to make new XML data with properties {name:"xmldata", text:theXMLdata}
return my getBlock(xmlData's text, "item", 1) -- Solution to part 1 of problem.
return my getElementValuesByName(xmlData's contents, "name") -- Solution to part 2 of problem.
return my getElementValuesByName(xmlData's contents, "price") -- Solution to part 3 of problem.
end tell</lang>Output for the three results (respectively):<lang AppleScript>"<item upc=\"123456789\" stock=\"12\">
<name>Invisibility Cream</name>
<price>14.50</price>
<description>Makes you invisible</description>
</item>"
 
{"Invisibility Cream", "Levitation Salve", "Blork and Freen Instameal", "Grob winglets"}
 
{"14.50", "23.99", "4.95", "3.56"}</lang>
 
=={{header|Bracmat}}==
Line 1,699 ⟶ 1,701:
//Grob winglets
</pre>
 
=={{header|Go}}==
Using the standard <code>encoding/xml</code> package:
Line 1,993 ⟶ 1,996:
}
}</lang>
 
=={{header|JavaScript}}==
{{works with|Firefox|2.0}}
Line 2,645 ⟶ 2,649:
print $x->findnodes_as_string('//price');
$x->findnodes('//name')->get_nodelist;</lang>
 
=={{header|Perl 6}}==
 
<lang perl6>use XML::XPath;
 
my $XML = XML::XPath.new(xml => q:to/END/);
<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>
END
 
put "First item:\n", $XML.find('//item[1]')[0];
 
put "\nPrice elements:";
.contents.put for $XML.find('//price').List;
 
put "\nName elements:\n", $XML.find('//name')».contents.join: ', ';</lang>
{{out}}
<pre>First item:
<item upc="123456789" stock="12"> <name>Invisibility Cream</name> <price>14.50</price> <description>Makes you invisible</description> </item>
 
Price elements:
14.50
23.99
4.95
3.56
 
Name elements:
Invisibility Cream, Levitation Salve, Blork and Freen Instameal, Grob winglets</pre>
 
=={{header|PHP}}==
Line 2,959 ⟶ 2,911:
;; => '("Invisibility Cream" "Levitation Salve" "Blork and Freen Instameal" "Grob winglets")
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<lang perl6>use XML::XPath;
 
my $XML = XML::XPath.new(xml => q:to/END/);
<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>
END
 
put "First item:\n", $XML.find('//item[1]')[0];
 
put "\nPrice elements:";
.contents.put for $XML.find('//price').List;
 
put "\nName elements:\n", $XML.find('//name')».contents.join: ', ';</lang>
{{out}}
<pre>First item:
<item upc="123456789" stock="12"> <name>Invisibility Cream</name> <price>14.50</price> <description>Makes you invisible</description> </item>
 
Price elements:
14.50
23.99
4.95
3.56
 
Name elements:
Invisibility Cream, Levitation Salve, Blork and Freen Instameal, Grob winglets</pre>
 
=={{header|Rascal}}==
Line 3,239 ⟶ 3,244:
puts [$node text]
}</lang>
 
=={{header|TUSCRIPT}}==
<lang tuscript>
Line 3,379 ⟶ 3,385:
Dim names = (From item In xml.XPathSelectElements("//name") Select item.Value).ToArray</lang>
 
 
=={{header|XProc}}==
Line 3,412 ⟶ 3,417:
</p:insert>
</p:pipeline></lang>
 
=={{header|XQuery}}==
<lang xquery>(:
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.