minor76
minor76

Reputation: 3

trying to parse specific data using xpath

I have a small xml file that I'm trying to grab the away_team first and then the home_team second. /game/team/statistics/@goals gives me the data I want but I need to reverse the order. So I'm trying to understand how to get the away_team goals first, followed by the home_team.

Below is the file

<game id="f24275a9-4f30-4a81-abdf-d16a9aeda087" status="closed" coverage="full" home_team="4416d559-0f24-11e2-8525-18a905767e44" away_team="44167db4-0f24-11e2-8525-18a905767e44" scheduled="2013-10-10T23:00:00+00:00" attendance="18210" start_time="2013-10-10T23:08:00+00:00" end_time="2013-10-11T01:32:00+00:00" clock="00:00" period="3" xmlns="http://feed.elasticstats.com/schema/hockey/game-v2.0.xsd">
  <venue id="bd7b42fa-19bb-4b91-8615-214ccc3ff987" name="First Niagara Center" capacity="18690" address="One Seymour H. Knox III Plaza" city="Buffalo" state="NY" zip="14203" country="USA"/>
  <team name="Sabres" market="Buffalo" id="4416d559-0f24-11e2-8525-18a905767e44" points="1">
    <scoring>
      <period number="1" sequence="1" points="1"/>
      <period number="2" sequence="2" points="0"/>
      <period number="3" sequence="3" points="0"/>
    </scoring>
    <statistics goals="1" assists="2" penalties="7" penalty_minutes="23" team_penalties="0" team_penalty_minutes="0" shots="27" blocked_att="14" missed_shots="8" hits="25" giveaways="5" takeaways="10" blocked_shots="7" faceoffs_won="22" faceoffs_lost="28" powerplays="1" faceoffs="50" faceoff_win_pct="44.0" shooting_pct="3.7" points="3">
      <powerplay faceoffs_won="2" faceoffs_lost="0" shots="0" goals="0" missed_shots="1" assists="0" faceoff_win_pct="100.0" faceoffs="2"/>
      <shorthanded faceoffs_won="3" faceoffs_lost="3" shots="1" goals="0" missed_shots="0" assists="0" faceoffs="6" faceoff_win_pct="50.0"/>
      <evenstrength faceoff_win_pct="40.5" missed_shots="7" goals="1" faceoffs_won="17" shots="26" faceoffs="42" faceoffs_lost="25" assists="2"/>
      <penalty shots="0" goals="0" missed_shots="0"/>
    </statistics>
    <shootout shots="0" missed_shots="0" goals="0" shots_against="0" goals_against="0" saves="0" saves_pct="0"/>
    <goaltending shots_against="33" goals_against="4" saves="29" saves_pct="0.879" total_shots_against="33" total_goals_against="4">
      <powerplay shots_against="0" goals_against="0" saves="0" saves_pct="0"/>
      <shorthanded shots_against="7" goals_against="0" saves="7" saves_pct="1.0"/>
      <evenstrength goals_against="4" saves_pct="0.846" shots_against="26" saves="22"/>
      <penalty shots_against="0" goals_against="0" saves="0" saves_pct="0"/>
      <emptynet goals_against="0" shots_against="0">
        <powerplay goals_against="0"/>
        <shorthanded goals_against="0"/>
        <evenstrength goals_against="0"/>
      </emptynet>
    </goaltending>

Upvotes: 0

Views: 44

Answers (1)

LarsH
LarsH

Reputation: 28004

Here's an XPath 2.0 expression that should do what you asked, yielding a sequence of two elements:

(/game/team[@id = /game/@home_team]/statistics/@goals,
 /game/team[@id = /game/@away_team]/statistics/@goals)

Credit to @Ian for sleuthing out the details of the question.

In XPath 1.0, you could concatenate string data from the two teams in whatever order you want:

concat(/game/team[@id = /game/@home_team]/statistics/@goals, ' ',
       /game/team[@id = /game/@away_team]/statistics/@goals)

But as Ian said, you can't produce a nodeset with an order different from document order. (I don't think a nodeset has any intrinsic order at all... it's how it's processed that imposes an order.)

Update:

As Ian pointed out, your XML data is in a namespace, thanks to the default namespace declaration on <game>. Since you said that "/game/team/statistics/@goals gives me the data", I'm assuming that you've already taken care of this aspect of the problem, perhaps by declaring the default namespace in your XPath execution environment.

Upvotes: 2

Related Questions