问题
I've built an Xpath expression by concatenating strings in VB6:
strXPath = "xDOC.selectNodes(" & """/GroupType1""" & ").item(" & CStr(i) & ").selectNodes(" & """/OperationStageCollection/OperationStage""" & ").length"
"i" is an integer used to index into
I want to evaluate strXPath to get a loop counter, for example:
n = CInt(strXPath)
n is declared as Integer; strXPath is declared as string. VB6 throws a Type Mismatch error on the above evaluation expression. I must be missing something obvious. How can I evaluate strXPath?
I realize that there may be errors in the XPath expression itself, but I'd like to get the evaluation working in order to debug such possible errors.
回答1:
Try removing some of the double-quotes:
iLength = xDOC.selectNodes("/GroupType1").item(i).selectNodes("/OperationStageCollection/OperationStage").length
This should return the length
property you want, as an Integer.
Then you can use iLength
in your loop.
回答2:
@BRW: both of your questions are very specific, i.e. how to achieve certain results using XPath. But I have the suspicion that if you would explain what (data) you try to retrieve form the XML, commenters might show you ways you didn't think of, e.g. say you want to iterate through all <OperationEvent>
s within a <OperationEventCollection>
, a single <OperationEvent>
can be retrieved by //GroupType1/OperationStageCollection/OperationStage/OperationEventCollection/OperationEvent[1-based-index]
, e.g. //GroupType1/OperationStageCollection/OperationStage/OperationEventCollection/OperationEvent[1]
, which results in a single XML node:
<OperationEvent>
<OperationEventDate1>2018-12-16</OperationEventDate1>
<OperationEventCode>5</OperationEventCode>
<OperationEventDate2>2018-05-16</OperationEventDate2>
</OperationEvent>
So instead multiple selectNodes
methods, one proper XPath query might yield the desired outcome right away.
来源:https://stackoverflow.com/questions/58576564/how-evaluate-concatenated-string-as-indexed-xpath-expression-in-vb6