XML/XPath: Difference between revisions

154 bytes removed ,  12 years ago
Line 72:
Unfortunately, System Events cannot accomplish the first goal, but it can be used to accomplish the final two:''
 
<lang AppleScript>on getXMLElementsByNamegetElementsByName(theElement, theNameToFind)
set R to {}
repeat with i in theElement
Line 82:
on error
set theValue to null
end try
try
repeat with j in i's XML attributes
set end of theAttributes to {name:j's name, value:j's value}
end repeat
end try
end tell
if (count of theElements) > 0 then set R to R & my getXMLElementsByNamegetElementsByName(theElements, theNameToFind)
if theName = theNameToFind then set R to R & theValue
end repeat
return R
end getElementsByName
end getXMLElementsByName
 
set theXMLdata to "<inventory title=\"OmniCorp Store #45x10^3\">
Line 125 ⟶ 120:
set xmlData to make new XML data with properties {name:"xmldata", text:theXMLdata}
return my getXMLElementsByNamegetElementsByName(xmlData's contents, "name")
--> {"Invisibility Cream", "Levitation Salve", "Blork and Freen Instameal", "Grob winglets"}
 
return my getXMLElementsByNamegetElementsByName(xmlData's contents, "price")
--> {"14.50", "23.99", "4.95", "3.56"}
end
Anonymous user