这是我的XML文件simple.xml
的精简版。
<Genealogy>
<grandParent>
<name>bob</name>
<surname>Carter</surname>
<Parent>
<tel_Number>0033689765498</tel_Number>
<mobile_Phone>2</mobile_Phone>
</Parent>
</grandParent>
<grandParent>
<name>jean</name>
<surname>Wright</surname>
<Parent>
<tel_Number>0033648245432</tel_Number>
<mobile_Phone>1</mobile_Phone>
</Parent>
</grandParent>
</Genealogy>
使用Xpath表达式,是否可以在不循环所有节点的情况下找到其属性为例的正确/特定节点//grandParent/Parent
:
Parent child of grandParent
tel_Number="0033648245432" name="jean"
答案 0 :(得分:1)
尝试像这样的XPath表达式:
//grandParent/Parent/tel_Number[text()='0033648245434' and ../../name/text()='jean']
代码如下:
name = "jean"
phone = "0033648245434"
Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.async = False
xml.load "C:\path\to\simple.xml"
expr = "//grandParent/Parent/tel_Number[text()='" & phone _
& "' and ../../name/text()='" & name & "']"
Set nodes = xml.selectNodes(expr)
For n In nodes
WScript.Echo n.parentNode.nodeName
Next