【发布时间】:2017-08-01 02:23:24
【问题描述】:
我的 xml 树在结构中包含这个
<foo name="bar">
<location order="1">data1</location>
<location order="2">data2</location>
</foo>
我试图引用树中位置顺序 =“2”(可能有 1000 个)的任何位置来操作所有 data2 文本。我无法通过 xpath 字符串分配让 vba 识别它。
我尝试了很多东西,但对我来说最有意义的是
xPath = "//prefix:foo[@name='bar']/location[@order='2']"
但是,如果我删除它,它确实会识别到 foo 的 xPath 分配。
"/location[@order='2']"
我在 xpath 语法中做错了吗?或者是否有更多的方法来为包含要在树结构中选择的多个属性的节点分配路径?
也许我试图使用错误的方法来访问变量?
Dim list as IXMLDOMNodeList
Set list = xDoc.SelectNodes(xPath)
Debug.Print list.length
给我一个 0,但我的 xml 中有两个特定节点的实例
编辑: 和你做了一些事情后仍然给我零。这是一个示例 xml,因此您可以查看命名空间。如果我省略“/location[@order='2']”,我仍然可以让它打印一个长度。另外澄清一下,我只对路径感兴趣,即可能有许多其他 foo 节点与 child 。这些我暂时不关心。
<?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>
【问题讨论】:
-
"foo[@name='bar']/location[@order='2']"为我工作-但您似乎没有在示例 XML 中包含一些命名空间,因此很难确定。你能让它更能代表你的实际用例吗? -
嗯。从我的手机打字并在没有访问权限的情况下在笔记本电脑上处理文件。给我一点,直到我这样做,我会看看我能做什么。目前,我可以告诉你命名空间在没有 location order="2" 节点的情况下工作得很好。
-
:/ 可能我用错了方法。
-
我正在尝试使用访问变量
-
对不起,我会编辑帖子...