Xpath:获取节点属性

时间:2014-05-15 21:09:16

标签: javascript xml xpath

我正在尝试解析看起来像这样的XML文件:

<body>
<predictions>
<direction>
    <prediction epochTime="1400182631361" seconds="1109" minutes="18" isDeparture="false" dirTag="N__OB1" vehicle="1495" vehiclesInConsist="2" block="9708" tripTag="6041030"/>
    <prediction epochTime="1400182942181" seconds="1420" minutes="23" isDeparture="false" dirTag="N__OB1" vehicle="1515" vehiclesInConsist="2" block="9709" tripTag="6041031"/>
    <prediction epochTime="1400183687220" seconds="2165" minutes="36" isDeparture="false" dirTag="N__OB1" vehicle="1481" vehiclesInConsist="2" block="9710" tripTag="6041032"/>
    <prediction epochTime="1400183966372" seconds="2444" minutes="40" isDeparture="false" affectedByLayover="true" dirTag="N__OB1" vehicle="1520" vehiclesInConsist="2" block="9712" tripTag="6041033"/>
    <prediction epochTime="1400184566372" seconds="3044" minutes="50" isDeparture="false" affectedByLayover="true" dirTag="N__OB1" vehicle="1430" vehiclesInConsist="2" block="9713" tripTag="6041034"/>
</direction>

</predictions>
</body>

我想用Xpath(通过“/ body / predictions / direction / @ seconds”)提取每个seconds节点内的所有prediction属性,但它不会为我返回任何内容。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

//prediction/@seconds

/选择所需的属性时,双@会选择所有行。您的查询没有返回任何内容,因为您尝试获取seconds的{​​{1}}属性,当然这种属性不存在。