我正在编写这个VBA脚本,用于自动生成自动化结果。我试图解析的节点示例如下:
> <test id="41">
> <name>7.1.1.4_BandI_PS</name>
> <ttcnTestCaseName>7.1.1.4</ttcnTestCaseName>
> <numberOfIterations>1</numberOfIterations>
> <failureAction selected="Continue"/>
> <runMode>Normal</runMode>
> <testPicsPixitDeviation>
> <picsPixitDeviationTag>BandI</picsPixitDeviationTag>
> <picsPixitDeviationTag>PS</picsPixitDeviationTag>
> <picsPixitDeviationTag>NonCipher</picsPixitDeviationTag>
> </testPicsPixitDeviation>
> <comment/>
> <result iterationIndex="0" repeatIndex="0">
> <status>
> <status>Passed</status>
> </status>
> <resultLocation>C:result_archMAC_D12wk47_v10-tc_7_1_1_4_2013-01-07_15.18.27</resultLocation>
> <startTime>2013-01-07_15.18.26</startTime>
> <executionDuration>120</executionDuration>
> <ptsIpAddress>127.0.0.1</ptsIpAddress>
> </result>
> </test>
> <test id="42">
> <name>7.1.1.8_BandI_CS</name>
> <ttcnTestCaseName>7.1.1.8</ttcnTestCaseName>
> <numberOfIterations>1</numberOfIterations>
> <failureAction selected="Continue"/>
> <runMode>Normal</runMode>
> <testPicsPixitDeviation>
> <picsPixitDeviationTag>BandI</picsPixitDeviationTag>
> <picsPixitDeviationTag>CS</picsPixitDeviationTag>
> <picsPixitDeviationTag>NonCipher</picsPixitDeviationTag>
> </testPicsPixitDeviation>
> <comment/>
> <result iterationIndex="0" repeatIndex="0">
> <status>
> <status>Passed</status>
> </status>
> <resultLocation>C:result_archMAC_D12wk47_v10-tc_7_1_1_8_2013-01-07_15.20.27</resultLocation>
> <startTime>2013-01-07_15.20.27</startTime>
> <executionDuration>104</executionDuration>
> <ptsIpAddress>127.0.0.1</ptsIpAddress>
> </result>
> </test>
从上面可以看出,根据迭代次数的不同,测试节点可以有同样多的结果。我使用了selectNodes方法来解析文件中的所有节点,这样可以重新获得正确数量的元素。对于我返回的列表中的每个测试用例,我都会进行解析,看看有多少个测试用例,并在嵌套的列表中为每个节点返回。问题是,列表没有返回嵌套在每个中的,而是返回文本文件中不应该返回的所有。我的代码如下。
Dim testCase As MSXML2.IXMLDOMNode
For Each testCase In testCaseNamesList
Dim passed, failed, error, totalRunTime, iterationCount As Integer
Dim passPcnt, failPcnt, errorPcnt, averageRunTime As Double
Dim testCaseName As String
Dim testCaseResultList As MSXML2.IXMLDOMNodeList
Set testCaseResultList = testCase.SelectNodes("//result")
MsgBox (testCaseResultList.Length)
testCaseName = testCase.FirstChild.Text
iterationCount = CInt(testCase.SelectSingleNode("//numberOfIterations").Text)
Dim testCaseResult As MSXML2.IXMLDOMNode
For Each testCaseResult In testCaseResultList
一切都正常,但变量testCaseResultList应该返回每个节点中包含的列表,但它却从其他节点返回。我不知道我做错了什么。
尝试.//result
,因为它将返回上下文节点(testCase
指向的节点)的所有"结果"子节点,或者只尝试result
,它将返回该上下文节点的所有"成果"子节点(但不会返回孙、曾孙等)
使用//result
返回文档根的所有"结果"子代,该子代将返回文档中的每个"结果"节点
类似地,//numberOfIterations
(返回文档根的所有"numberOfIterations"子代)应该由.//numberOfIterations
或仅由numberOfIterations
替换
请参阅上的缩写语法指南http://www.w3.org/TR/xpath/#path-缩写了解更多详细信息