2015-06-03 38 views
0

在Excel 2007中使用VBA,MS XML參考(v6.0)和xpath我試圖提取包含集合中最後一個節點的節點列表。該XML看起來是這樣的:使用xpath last()函數提取節點列表中的最後一個項目

<Routes> 
    <Route id="RT1"> 
    <PrivateCode>PBAO902:3</PrivateCode> 
    <Description>City Centre, Corporation St - Hill Hook, Bishops Way</Description> 
    <RouteSectionRef>RS1</RouteSectionRef> 
    <RouteSectionRef>RS2</RouteSectionRef> 
    <RouteSectionRef>RS3</RouteSectionRef> 
    <RouteSectionRef>RS4</RouteSectionRef> 
    <RouteSectionRef>RS5</RouteSectionRef> 
    </Route> 
    <Route id="RT2"> 
    <PrivateCode>PBAO904:3</PrivateCode> 
    <Description>City Centre, Corporation St - Falcon Lodge, Churchill Parade</Description> 
    <RouteSectionRef>RS1</RouteSectionRef> 
    <RouteSectionRef>RS2</RouteSectionRef> 
    <RouteSectionRef>RS6</RouteSectionRef> 
    <RouteSectionRef>RS7</RouteSectionRef> 
    </Route> 
    <Route id="RT3"> 
    <PrivateCode>PBAO905:3</PrivateCode> 
    <Description>City Centre, Corporation St - Roughley, Slade Road</Description> 
    <RouteSectionRef>RS1</RouteSectionRef> 
    <RouteSectionRef>RS2</RouteSectionRef> 
    <RouteSectionRef>RS3</RouteSectionRef> 
    <RouteSectionRef>RS4</RouteSectionRef> 
    <RouteSectionRef>RS8</RouteSectionRef> 
    </Route> 
<Routes> 

我想提取,作爲一個節點列表,第一RouteSectionRef每條路線(RS1,RS1,RS1),並作爲一個獨立的節點列表中的最後RouteSectionRef每條路線( RS5,RS7,RS8)。我可以用提取的第一個執行以下操作:

strPath = "//Routes/Route/RouteSectionRef[0]" 
Set dNL = dom.DocumentElement.SelectNodes(strPath) 
n = 0 
For Each dN In dNL 
    n = n + 1 
    arrRouteDescriptions(n, 2) = dN.Text 
Next dN 

我使用以下提取最後:

strPath = "//Routes/Route/RouteSectionRef[last()]" 
Set dNL = dom.DocumentElement.SelectNodes(strPath) 
n = 0 
For Each dN In dNL 
    n = n + 1 
    arrRouteDescriptions(n, 3) = dN.Text 
Next dN 

但收到以下運行時錯誤:

未知方法 // Routes/Route/RouteSectionRef [ - > last()< - ]

任何人都可以指向正確的方向嗎?

回答

0

聽起來好像您正在使用舊版本的MSXML,其默認選擇語言不是XPath 1.0。因此,成立

dom.setProperty "SelectionLanguage", "XPath" 

,以確保你做任何selectNodesselectSingleNode調用之前啓用的XPath。

相關問題