Xpath语法以选择路径中具有多个属性的节点 [英] Xpath syntax to select nodes with multiple attributes in the path
问题描述
我的xml树在结构中包含了这个
My xml tree contains this in the structure
<foo name="bar">
<location order="1">data1</location>
<location order="2">data2</location>
</foo>
我正在尝试引用树中位置顺序="2"(可能有1000个)的任何地方来操纵所有data2文本.我无法通过xpath字符串分配让vba识别它.
I'm trying to refer to anywhere in the tree where location order ="2" (there could be 1000s of them) to manipulate all data2 texts. I cannot get vba to recognize it with an xpath string assignment.
我尝试了很多事情,但是使我觉得最有意义的事情是
I've tried many things but the thing that makes the makes the most sense to me is
xPath = "//prefix:foo[@name='bar']/location[@order='2']"
但是,如果我将其删除,它确实会识别到foo的xPath分配.
It does however recognize the xPath assignment to foo if I remove.
"/location[@order='2']"
我在xpath语法中做错了吗?还是有更多的方法可以为包含几个要在树形结构中选择的属性的节点分配路径?
Am I doing something wrong in the xpath syntax? Or is there more to assigning a path to a node containing several attributes to be selected in the tree structure?
也许我正在尝试使用错误的方法来访问变量?
Maybe I'm trying to use the wrong methods to access the variable?
Dim list as IXMLDOMNodeList
Set list = xDoc.SelectNodes(xPath)
Debug.Print list.length
给我一个0,但是我的xml中有两个特定节点的实例
Gives me a 0 but there are two instances of those specific nodes in my xml
在和你做一些事后仍然给我零.这是一个示例xml,因此您可以看到名称空间.如果省略"/location [@ order ='2']",我仍然可以打印长度.同样要澄清的是,我只对路径感兴趣,即child可能有许多其他foo节点.这些我现在不在乎.
Still giving me zero after doing some things with yours. Here's an example xml so you can see namespace. I can still get it to print a length if I leave out the "/location[@order='2']". Also to clarify, I only am interested in the path that is , there could be many other foo nodes with the child . These I do not care about for now.
<?xml version="1.0" encoding="utf-8"?>
<IPSGDatas xsi:schemaLocation="uri:mybikes:wheels MYBIKES%20WHEELS%202012.xsd"
xmlns="uri:mybikes:wheels"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<language_id>120</language_id>
</header>
<datas>
<good>
<signature/>
<bike>
<foos>
<marker>
<location order="1">data1</location>
<location order="2">data2</location>
</marker>
<foo name="bar">
<location order="1">data1</location>
<location order="2">data2</location>
</foo>
</foos>
<profile_id>MyName1</profile_id>
</bike>
<action_id>New</action_id>
<index_id>1</index_id>
<agency/>
<agency_reference/>
<accreditation_id>U</accreditation_id>
</good>
<good>
<signature/>
<bike>
<foos>
<marker>
<location order="1">data1</location>
<location order="2">data2</location>
</marker>
<foo name="bar">
<location order="1">data1</location>
<location order="2">data2</location>
</foo>
</foos>
<profile_id>MyName2</profile_id>
</bike>
<action_id>New</action_id>
<index_id>1</index_id>
<agency/>
<agency_reference/>
<accreditation_id>U</accreditation_id>
</good>
</datas>
</IPSGDatas>
推荐答案
已编辑
使用更新后的XML,此代码将"data2"和"data2"作为输出:
Using your updated XML, this code gives me "data2" and "data2" as output:
Sub Tester3()
Dim xmlDoc As New MSXML2.DOMDocument30
Dim objNodes As IXMLDOMNodeList, o As Object
xmlDoc.async = False
xmlDoc.LoadXML Range("C1").Value
xmlDoc.setProperty "SelectionLanguage", "XPath"
'### this takes care of the namespace ###
xmlDoc.setProperty "SelectionNamespaces", _
"xmlns:xx='uri:mybikes:wheels'"
If xmlDoc.parseError.errorCode <> 0 Then
MsgBox "Error!" & vbCrLf & _
" Line: " & xmlDoc.parseError.Line & vbCrLf & _
" Text:" & xmlDoc.parseError.srcText & vbCrLf & _
" Reason: " & xmlDoc.parseError.reason
Else
'### note: adding the namespace alias prefix defined above ###
Set objNodes = xmlDoc.SelectNodes("//xx:foo[@name='bar']/xx:location[@order='2']")
If objNodes.Length = 0 Then
Debug.Print "not found"
Else
For Each o In objNodes
Debug.Print o.nodeTypedValue
Next o
End If 'have line items
End If 'parsed OK
End Sub
以前类似的问:如何忽略XML名称空间
这篇关于Xpath语法以选择路径中具有多个属性的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!