-1

这是我的 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"
4

1 回答 1

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
于 2013-04-29T23:23:32.873 回答