问题
I'm working with xml files having the structure set out below.
<GroupType1> and <GroupType2> need to be processed separately.
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
And then I'd do the same for <GroupType2>
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
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>?
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.
Here's the xml structure:
<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>
    回答1:
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>.
来源:https://stackoverflow.com/questions/58530466/xpath-how-get-length-of-1st-element-group-and-length-of-2nd-element-group-ins