尝试使用xpath解析特定数据

时间:2014-10-10 17:21:32

标签: xpath

我有一个小的xml文件,我试图首先抓住away_team,然后是home_team秒。 / game / team / statistics / @ goals给了我想要的数据,但我需要撤销订单。所以我试图了解如何首先获得away_team目标,然后是home_team。

以下是文件

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

1 个答案:

答案 0 :(得分:2)

这是一个XPath 2.0表达式,应该按照你的要求进行操作,产生一系列两个元素:

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

感谢@Ian调查问题的详细信息。

在XPath 1.0中,您可以按照您想要的顺序连接两个团队的字符串数据:

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

但正如Ian所说,您无法生成订单与订单不同的节点集。 (我认为一个节点集根本没有任何内在的顺序......它是如何处理的,它会强加一个订单。)

更新

正如Ian指出的那样,由于<game>上的默认命名空间声明,您的XML数据位于命名空间中。既然你说“/ game / team / statistics / @ goals给了我数据”,我假设你已经解决了问题的这个方面,可能是通过在XPath执行环境中声明默认命名空间。