Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

david.cherrie

macrumors newbie
Original poster
Jun 17, 2015
7
0
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?

Code:
<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

Code:
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
 
Hi,

If I understand you correctly then I think this will work:

Code:
tell application "System Events"
   set xmlData to contents of XML file "/xmlfile.xml"
   tell XML element "rugbymatch" of xmlData
       tell XML element "home_players"
           set thePlayers to every XML element whose value of XML element "player_no" is "3"
       end tell
   end tell
end tell
 
Thanks for the reply and yes you are on the right track but am having a bit of a problem. Am new to AppleScript so forgive me. I don't understand what object theplayers refers to and how it is inserted into the script.

Code:
tell application "System Events"
    set xmlData to contents of XML file "/xmlfile.xml"
    tell XML element "rugbymatch" of xmlData
        tell XML element "home_players"
            set thePlayers to every XML element whose value of XML element "player_no" is "3"
            set KickerNumber to value of XML element "player_no"
            set KickerName to value of XML element "player_short"
            set KickerConv to value of XML element "conv"
            set KickerConvAtt to value of XML element "conv_att"
        end tell
    end tell
end tell

I get ERROR: System Events got an error: Can't get XML element "player_no" of XML element of XML element "home_players" of XML element "rugby match" of contents of XML file "xmlfile.xml". (-1728)

At the moment I'm using tell (10th XML element whose name is "player") as the quick fix however this relys on the location that the number 10 player is 10th in the list.

Cheers,
 
Hi, "thePlayers" is a list of all players with player number 3. I assume that there would only ever be one player number with the player number 3, so it's a list with just the one item. So if you then wanted to get the player_short of the player with number three then you'd do something like this:

Code:
tell application "System Events"
   set xmlData to contents of XML file "/xmlfile.xml"
   tell XML element "rugbymatch" of xmlData
       tell XML element "home_players"

           set thePlayers to every XML element whose value of XML element "player_no" is "3"
           set thePlayer to item 1 of thePlayers--get the first item of our list
          
           --we now have a single XML element called thePlayer and we can pull out info as we see fit
           set theKickerName to value of XML element "player_short" of thePlayer

       end tell
   end tell
end tell
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.