XPATH:如何获得第一个元素组的 .length 和第一组内第二个元素组的 .length? [英] XPATH: how get .length of 1st element group and .length of 2nd element group inside 1st group?
问题描述
我正在处理具有以下结构的 xml 文件.
I'm working with xml files having the structure set out below.
和
需要分别处理.
<GroupType1>
and <GroupType2>
need to be processed separately.
无论哪种情况,我都想使用 XPATH 遍历每个
中的
元素.
In either case, I want to loop through the <OperationEvent>
elements in each <OperationStage>
using XPATH.
所以,对于每个
,我需要在
中获取
的数量,然后获取当前
的
中
的个数.
So, for each <GroupType1>
, I need to get the number of <OperationStage>
in <OperationStageCollection>
, then get the number of <OperationEvent>
in the <OperationEventCollection>
of the current <OperationStage>
.
在伪代码中,处理将是:
In pseudocode, the processing would be:
For i = 1 to number of <OperationStageCode> in current <OperationStage> of current <GroupType1>
For j = 1 to number of <OperationEvent> in ith <OperationStage> in the current <GroupType1>
process jth <OperationEvent> of ith <OperationStage>
Next J
Next i
然后我会为
我一直在尝试获取
中
的数量,以及
的数量当前
的
中的code>.
I'm stuck trying to get the number of <OperationStage>
in <OperationStageCollection>
, and the number of <OperationEvent>
in the <OperationEventCollection>
of the current <OperationStage>
.
我尝试了各种表达方式,例如:
I've tried various expressions, like:
i = xDOC.selectNodes("//OperationStage").length
但当然这只是获取整个
中
的总数.我尝试过的其他表达式(如下)都返回零.正如您所看到的,我只是在这里胡扯——暴露了我对 XPATH 遍历语法的不熟悉.
But of course that just gets the total number of <OperationStage>
in the entire <GroupCollection>
. The other expressions I've tried (below) all return zero. As you can see, I'm just flailing here--exposing my lack of familiarity with XPATH traversal syntax.
i = xDOC.selectNodes("/OperationStage").length
i = xDOC.selectNodes("OperationStageCollection//catmk:ProceedingStage").length
i = xDOC.selectNodes("OperationStageCollection/catmk:ProceedingStage").length
i = xDOC.selectNodes("/OperationStageCollection/catmk:ProceedingStage").length
在单个
的
中获取
数量的正确语法是什么,然后获取当前
的
中
的个数?
What is the correct syntax to get the number of <OperationStage>
in <OperationStageCollection>
of a single <GroupType1>
, then get the number of <OperationEvent>
in the <OperationEventCollection>
of the current <OperationStage>
?
我查看了很多 XPATH 文档,但在我的情况下,我没有找到任何有用的示例.如果您知道此类文档,也请指点我.
I've looked at a lot of XPATH documentation, but I haven't found anything with useful examples in the ballpark of my situation. Please also point me to such documentation if you're aware of it.
这里是xml结构:
<GroupCollection>
<Groups>
<GroupType1>
<GroupType1_Identifier>1</GroupType1_Identifier>
<OperationStageCollection>
<OperationStage>
<OperationStageCode>3</OperationStageCode>
<OperationEventCollection>
<OperationEvent>
<OperationEventDate1>2018-12-16</OperationEventDate1>
<OperationEventCode>5</OperationEventCode>
<OperationEventDate2>2018-05-16</OperationEventDate2>
</OperationEvent>
... more OperationEvents ...
</OperationEventCollection>
</OperationStage>
... more OperationStages ...
</OperationStageCollection>
</GroupType1>
...moreGroupType1...
<GroupType2>
<GroupType2_Identifier>3</GroupType2_Identifier>
<OperationStageCollection>
<OperationStage>
<OperationStageCode>101</OperationStageCode>
<OperationEventCollection>
<OperationEvent>
<OperationEventCode>6</OperationEventCode>
<OperationEventDate2>2012-01-03</OperationEventDate2>
</OperationEvent>
... more OperationEvents ...
</OperationEventCollection>
</OperationStage>
... more OperationStages ...
</OperationStageCollection>
</GroupType2>
...moreGroupType2...
</Groups>
</GroupCollection>
推荐答案
严格来说,xpath 表达式,我相信
Strictly in terms of xpath expressions, I believe
//GroupType1/OperationStageCollection/count(OperationStage)
应该为您提供单个
和
的数量>
should get you the number of <OperationStage>
s in <OperationStageCollection>
of a single <GroupType1>
and
//GroupType1/OperationStageCollection/OperationStage//OperationEventCollection/count(OperationEvent)
应该为您提供当前
的
中
的数量.
should get you the number of <OperationEvent>
s in the <OperationEventCollection>
of the current <OperationStage>
.
这篇关于XPATH:如何获得第一个元素组的 .length 和第一组内第二个元素组的 .length?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!