2013-04-29 105 views
-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" 

回答

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 
相關問題