Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript Multiple XML Paths of Same Name

Hi there,


I have an XML feed that contains multiple paths with the same name. Is there a way to tell AppleScript to go into the 3rd or 4th path of the same name.


See below XML feed. Is there a way to either tell AppleScript to return the player number 2 by telling AppleScript to go into the second XML element of <player> cause currently I can only return the first player because its the first <player> it comes across and enters into that folder path. Or can I search for the <player_no> that equals the number I want to return?


<rugbymatch>

<home_players>

<player>

<player_id>3986</player_id>

<player_name>James Slipper</player_name>

<player_short>J. Slipper</player_short>

<surname>Slipper</surname>

<other_names>James</other_names>

<player_no>1</player_no>

<player_pos>LHP</player_pos>

</player>

<player>

<player_id>1038</player_id>

<player_name>Saia Fainga'a</player_name>

<player_short>S. Fainga'a</player_short>

<surname>Fainga'a</surname>

<other_names>Saia</other_names>

<player_no>2</player_no>

<player_pos>HOOK</player_pos>

</player>

<player>

<player_id>2173</player_id>

<player_name>Sekope Kepu</player_name>

<player_short>S. Kepu</player_short>

<surname>Kepu</surname>

<other_names>Sekope</other_names>

<player_no>3</player_no>

<player_pos>THP</player_pos>

</player>

</home_players>

</rugbymatch>


Here is my AppleScript


tell application "System Events"

set xmlData to contents of XML file "/xmlfile.xml"

tell XML element "rugbymatch" of xmlData

tell XML element "home_players"

tell XML element "player"

set homeKickerName to value of XML element "player_name"

end tell

end tell

end tell

end tell

end tell

MacBook Pro (Retina, 13-inch, Late 2013), OS X Mavericks (10.9.1)

Posted on Jun 17, 2015 4:56 PM

Reply
8 replies

Jun 17, 2015 8:29 PM in response to David.Cherrie

Hi,


Use a filter to get a list of players : whose name is "player"

Use an index to get the nth player : item Index



Like this:

------------

tell application "System Events"

tell contents of XML file "/xmlfile.xml"

tell (XML elements of XML element "home_players" of XML element "rugbymatch" whose name is "player") -- a list of players

set homeKickerName to value of XML element "player_name" of item 2 -- item 2 is the second "player"

end tell

end tell

end tell

Jun 18, 2015 5:22 PM in response to Jacques Rioux

Thanks for your reply. After I made this post I found a similar way of what you posted by using the following:


tell (10th XML element whose name is "player")


However this relys on the number 10 player being 10th in the list. Is there anyway for it to search all the "player_no" and get the value from that rather than relying on the positioning of the list?


Forgive me but I'm new to AppleScript.


Thanks so much for your help!

Jun 18, 2015 8:06 PM in response to David.Cherrie

Hi,


David.Cherrie wrote:


Is there anyway for it to search all the "player_no" and get the value from that rather than relying on the positioning of the list?


Yes, it's possible with another filter :

--------

tell application "System Events"

tell contents of XML file "/xmlfile.xml"

tell (first XML element of XML element "home_players" of XML element "rugbymatch" whose name is "player" and value of XML element "player_no" is "2")

set homeKickerName to value of XML element "player_name"

end tell

end tell

end tell

Jul 1, 2015 6:02 PM in response to Jacques Rioux

Sorry I never replied but it works perfectly. Have been using it a lot.


I have a follow up question though which is the complete opposite of the above and ignore the parent elements. I have an XML feed that looks like this:


<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<GFX xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<FS01>

<Description>Dog Image 1</Description>

<FileName>FS01.png</FileName>

</FS01>

<FS02>

<Description>Dog Image 2</Description>

<FileName>FS02.png</FileName>

</FS02>

<FS03>

<Description>Cat Image</Description>

<FileName>FS02.png</FileName>

</FS03>


I would ideally like to create an AppleScript to ask for a dialog to search for and return a list of codes (FS01, FS02, FS03). Is there a way I can create a display dialog to search for all Descriptions that have the word (say Dog) in it and return a list of values that are FS01, FS02. I would then use that list to display dialog that list so you could then choose FS01 or FS02.


Cheers,


Dave

Jul 1, 2015 6:46 PM in response to David.Cherrie

You could try something like this (I'm reading from a string instead of xml file but approach is the same):


SG



set theData to "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>

<GFX xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">

<FS01>

<Description>Dog Image 1</Description>

<FileName>FS01.png</FileName>

</FS01>

<FS02>

<Description>Dog Image 2</Description>

<FileName>FS02.png</FileName>

</FS02>

<FS03>

<Description>Cat Image</Description>

<FileName>FS02.png</FileName>

</FS03>

</GFX>"


display dialog "Search in description for:" default answer ""

set searchTerm to text returned of result

tell application "System Events"

set xmlData to makenewXML datawith properties {name:"xmldata", text:theData}

tell XML element "GFX" of xmlData

set filteredCodes to name of ¬

(XML elements whose value of XML element "Description" contains searchTerm)


-->{"FS01", "FS02"} if search term is "dog"

end tell

end tell

set myChoice to (choose from listfilteredCodes) as text-->"FS02"

Jul 1, 2015 7:38 PM in response to David.Cherrie

David.Cherrie wrote:


Is there a way to return the description in the choose from list but then only set myChoice to the filtered code and not description?


Something like this?


SG


set theData to "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>

<GFX xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">

<FS01>

<Description>Dog Image 1</Description>

<FileName>FS01.png</FileName>

</FS01>

<FS02>

<Description>Dog Image 2</Description>

<FileName>FS02.png</FileName>

</FS02>

<FS03>

<Description>Cat Image</Description>

<FileName>FS02.png</FileName>

</FS03>

</GFX>"


display dialog "Search in description for:" default answer ""

set searchTerm to text returned of result

tell application "System Events"

set xmlData to makenewXML datawith properties {name:"xmldata", text:theData}

tell XML element "GFX" of xmlData

set filteredDescr to value of XML element "Description" of ¬

(XML elements whose value of XML element "Description" contains searchTerm)


-->{"Dog Image 1", "Dog Image 2"} if search term is "dog"

tell me to set myDescrChoice to (choose from list filteredDescr) as text

set myCodeChoice to name of first XML element whose value of XML element "Description" is myDescrChoice

end tell

end tell

AppleScript Multiple XML Paths of Same Name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.