【发布时间】:2019-10-23 20:21:02
【问题描述】:
我正在处理具有以下结构的 xml 文件。
<GroupType1>和<GroupType2>需要分开处理。
无论哪种情况,我都想使用 XPATH 遍历每个 <OperationStage> 中的 <OperationEvent> 元素。
所以,对于每个<GroupType1>,我需要在<OperationStageCollection>中获取<OperationStage>的数量,然后在当前<OperationStage>的<OperationEventCollection>中获取<OperationEvent>的数量。
在伪代码中,处理将是:
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>做同样的事情
我一直在尝试获取<OperationStageCollection> 中的<OperationStage> 的数量,以及当前<OperationStage> 的<OperationEventCollection> 中的<OperationEvent> 的数量。
我尝试过各种表达方式,比如:
i = xDOC.selectNodes("//OperationStage").length
当然,这只是得到整个<GroupCollection> 中<OperationStage> 的总数。我尝试过的其他表达式(如下)都返回零。正如你所看到的,我只是在这里胡闹——暴露了我对 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
获取单个<GroupType1>的<OperationStageCollection>中<OperationStage>的数量,然后获取当前<OperationStage>的<OperationEventCollection>中<OperationEvent>的数量的正确语法是什么?
我查看了很多 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>
【问题讨论】:
标签: xml xpath xml-parsing traversal