来自 xpath 查询的子集? [英] Subset from xpath query?
问题描述
我有大致如下的 XML:
I have XML that looks roughly as follows:
<Dispatch ID = "JJJJ">
<Log ID = "150504BCFSP00072">
<LogTime>"May 4 2015 5:48PM"</LogTime>
<LogType>"1125-KKKK"</LogType>
<LogDetails>
<details>
<DetailTime>"May 4 2015 5:48PM"</DetailTime>
<IncidentDetail>"[1] The Thing I Want"</IncidentDetail>
<DetailTime>"May 4 2015 5:49PM"</DetailTime>
<IncidentDetail>"[2] The Thing I Don't Want"</IncidentDetail>
</details>
</LogDetails>
</Log>
</Dispatch>
我想从站点 ID 为 JJJJ 的日志中提取所有 IncidentDetail 文本,&其中,它的 LogType 包含KKKK",它的 IncidentDetail 以 [1] 开头(使其成为可能很多的第一个更新).到目前为止,我有这个 XPATH 查询:
I want to extract all IncidentDetail text from the log where the Station ID is JJJJ, & where its LogType contains "KKKK", and its IncidentDetail starts with [1] (making it the first update of potentially many). So far I have this XPATH query:
//Dispatch[@ID='JJJJ']//Log/LogType[contains(.,'KKKK')]
这会为我提供事件列表,如下所示:
This gets me a list of incidents, as follows:
Element='<LogType>"1125-KKKK"</LogType>'
Element='<LogType>"1125-KKKK"</LogType>'
Element='<LogType>"1125-KKKK"</LogType>'
但这只会让我陷入困境.我现在如何返回并获取与我要查找的内容相匹配的每个 LogType 事件的 IncidentDetail 文本?是否有一些多步骤的过程?
But that's only got me in a rathole. How do I go backward now and get the IncidentDetail text for each of these LogType incidents that match what I'm looking for? Is there some multi-step process?
感谢任何帮助.
推荐答案
你可以把它作为谓词,像这样:
Instead of stepping to Log/LogType
in the main axis, you can put it as predicate like this :
//Dispatch[@ID='JJJJ'][Log/LogType[contains(.,'KKKK')]]
或其他方式:
//Dispatch[@ID='JJJJ' and Log/LogType[contains(.,'KKKK')]]
从这里开始,您可以轻松地继续主轴到达IncidentDetail
,而无需任何后退:
From here, you can easily continue the main axis to get to IncidentDetail
without having to go backward whatsoever :
/Log/LogDetails/details/IncidentDetail
关于更新:
您可以使用 xpath 索引添加过滤器以仅获取同一父级中的第一个 IncindentDetail
:
You can add filter to get only the first IncindentDetail
within the same parent either by using xpath index :
/Log/LogDetails/details/IncidentDetail[1]
或按节点的文本内容过滤:
or filtering by the node's text content :
/Log/LogDetails/details/IncidentDetail[contains(.,'[1]')
这篇关于来自 xpath 查询的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!