在使用xpath(时间不长)时,我遇到了一些奇怪的事情.
xml的缩短版本(完整的xml在这里,并且在pastebin上有一个快照):
<?xml version="1.0" encoding="utf-8" ?>
<body copyright="All data copyright San Francisco Muni 2013.">
<route tag="all">
<message id="10268" creator="jflynn" startBoundary="1378121400000" startBoundaryStr="Mon, Sep 02 04:30:00 PDT 2013" endBoundary="1378191540000" endBoundaryStr="Mon, Sep 02 23:59:00 PDT 2013" sendToBuses="false">
<text>Sunday schedules today.</text>
</message>
</route>
<route tag="44">
<message id="10221" creator="mlee" startBoundary="1377525600000" startBoundaryStr="Mon, Aug 26 07:00:00 PDT 2013" endBoundary="1382857140000" endBoundaryStr="Sat, Oct 26 23:59:00 PDT 2013" sendToBuses="false">
<routeConfiguredForMessage tag="44"> <stop tag="6420" title="Silver Ave & Revere Ave" />
</routeConfiguredForMessage>
<text>Stop moved across Revere During Construction</text>
</message>
<message id="10222" creator="mlee" startBoundary="1377525600000" startBoundaryStr="Mon, Aug 26 07:00:00 PDT 2013" endBoundary="1382857140000" endBoundaryStr="Sat, Oct 26 23:59:00 PDT 2013" sendToBuses="false">
<routeConfiguredForMessage tag="44"> <stop tag="6420" title="Silver Ave & Revere Ave" />
</routeConfiguredForMessage>
<text>Stop moved across Revere During Construction</text>
</message>
</route>
</body>
Run Code Online (Sandbox Code Playgroud)
表达方式
//route[1]
Run Code Online (Sandbox Code Playgroud)
route像我预期的那样返回第一个节点.但是,在尝试选择第一个message节点时,使用
//message[1]
Run Code Online (Sandbox Code Playgroud)
message返回多个节点而不是一个.
起初我认为这是一个平台问题,但在Android,桌面Java和几个在线xpath测试仪上进行测试,我得到了相同的结果.
可能是什么问题呢?
两个表达式分别代表其父级的第一个route和message子级.1你们所有的route兄弟姐妹都是兄弟姐妹共享一个body父母,所以第一个被归还,只有那个.但是每个子节点都route包含自己的一组message子route节点,其中第一个子节点是为每个节点返回的.
如果需要匹配message整个XML文档中的第一个元素,请使用:
(//message)[1]
Run Code Online (Sandbox Code Playgroud)
括号告诉处理器找到匹配的节点//message,然后[1]选择那些节点中的第一个节点之后出现的谓词.没有它们,[1]谓词将仅根据其父节点的子节点进行操作.
1 因为我是一个CSS选择器junkie:你的XPath表达式的选择器对应物分别是route:nth-of-type(1)和message:nth-of-type(1).
| 归档时间: |
|
| 查看次数: |
2639 次 |
| 最近记录: |