xml - XPATH:如何获得第一个元素组的 .length 和第一个组内的第二个元素组的 .length?
问题描述
我正在使用具有以下结构的 xml 文件。
<GroupType1>
并且<GroupType2>
需要单独处理。
无论哪种情况,我都想使用 XPATH遍历<OperationEvent>
每个元素中的元素。<OperationStage>
因此,对于每个<GroupType1>
,我需要获取 in 的数量<OperationStage>
,<OperationStageCollection>
然后获取当前的<OperationEvent>
in的数量。<OperationEventCollection>
<OperationStage>
在伪代码中,处理将是:
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
然后我也会这样做<GroupType2>
我一直在尝试获取<OperationStage>
in的<OperationStageCollection>
数量和current的数量。<OperationEvent>
<OperationEventCollection>
<OperationStage>
我尝试了各种表达方式,例如:
i = xDOC.selectNodes("//OperationStage").length
但当然,这只是获得<OperationStage>
整个<GroupCollection>
. 我尝试过的其他表达式(如下)都返回零。正如您所看到的,我只是在这里胡思乱想——暴露了我对 XPATH 遍历语法的不熟悉。
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
获取单个的<OperationStage>
in数量,然后获取当前的in the数量的正确语法是什么?<OperationStageCollection>
<GroupType1>
<OperationEvent>
<OperationEventCollection>
<OperationStage>
我查看了很多 XPATH 文档,但在我的情况下,我没有找到任何有用的示例。如果您知道的话,也请指点我这些文档。
这是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 表达式,我相信
//GroupType1/OperationStageCollection/count(OperationStage)
应该为您提供单个和中的<OperationStage>
s数量<OperationStageCollection>
<GroupType1>
//GroupType1/OperationStageCollection/OperationStage//OperationEventCollection/count(OperationEvent)
应该让你知道current中<OperationEvent>
s的数量。<OperationEventCollection>
<OperationStage>