在Excel 2007中使用VBA,MS XML参考(v6.0)和xpath尝试提取由集合中的最后一个节点组成的节点列表。 XML看起来像这样:
<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>
我想提取每个Route(RS1,RS1,RS1)中的第一个RouteSectionRef作为节点列表,并且作为单独的节点列表提取每个Route(RS5,RS7,RS8)中的最后一个RouteSectionRef。我可以使用以下内容提取第一个:
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
但收到以下运行时错误:
未知方法 //路由/路由/ RouteSectionRef [ - &GT;最后()&LT; - ]
有人能指出我正确的方向吗?
答案 0 :(得分:0)
听起来好像您使用的是旧版本的MSXML,默认选择语言不是XPath 1.0。所以设置
dom.setProperty "SelectionLanguage", "XPath"
确保在执行任何selectNodes
或selectSingleNode
来电之前启用XPath。