要用作另一个节点索引的XML节点的VBScript位置
我有以下XML:要用作另一个节点索引的XML节点的VBScript位置,xml,xpath,vbscript,wsh,Xml,Xpath,Vbscript,Wsh,我有以下XML: adapt.xml: <Adapters> <Adapter> <IPAddresses> <IPAddress>1.1.1.1</IPAddress> <IPAddress>2.2.2.2</IPAddress> <IPAddress>3.3.3.3</IPAddress> </IPAddress
adapt.xml:
<Adapters>
<Adapter>
<IPAddresses>
<IPAddress>1.1.1.1</IPAddress>
<IPAddress>2.2.2.2</IPAddress>
<IPAddress>3.3.3.3</IPAddress>
</IPAddresses>
<IPSubnets>
<IPSubnet>255.0.0.0</IPSubnet>
<IPSubnet>255.255.0.0</IPSubnet>
<IPSubnet>255.255.255.0</IPSubnet>
</IPSubnets>
</Adapter>
</Adapters>
我当前的输出是:
ipNode 1.1.1.1
subnet 255.0.0.0
ipNode 2.2.2.2
subnet 255.0.0.0
ipNode 3.3.3.3
subnet 255.0.0.0
ipNode 1.1.1.1
subnet 255.0.0.0
ipNode 2.2.2.2
subnet 255.255.0.0
ipNode 3.3.3.3
subnet 255.255.255.0
我想要的输出是:
ipNode 1.1.1.1
subnet 255.0.0.0
ipNode 2.2.2.2
subnet 255.0.0.0
ipNode 3.3.3.3
subnet 255.0.0.0
ipNode 1.1.1.1
subnet 255.0.0.0
ipNode 2.2.2.2
subnet 255.255.0.0
ipNode 3.3.3.3
subnet 255.255.255.0
我试图让xpath选择IPSubnet元素相对于IPAddress位置的相同位置
显然,count(前面的sibling::IPAddress)并没有做我认为应该做的事情
这似乎应该是可行的,因为节点上下文知道它自己的上一个和下一个同级,但当我尝试将其用作IPSubnet[x]的索引时,它并没有给出所需的结果
我需要在XPath字符串中执行此操作,而不是通过更改程序来执行诸如迭代节点和使用计数器或节点和子节点的长度之类的操作
有什么建议吗?我认为你的假设不正确。通过执行
//IPSubnet/IPSubnet
操作,节点上下文更改为
。在这种情况下,调用count(前面的同级::IPAddress)
总是产生0
,因为XML中的
从来没有任何前面的同级
。这就是为什么在每次迭代中都会得到//IPSubnet/IPSubnet[1]
——它的值是255.255.255.0
我能得到的最接近的东西是这样的:
For Each ipNode In adaptersDoc.SelectNodes("//IPAddresses/IPAddress")
WScript.Echo "ipNode", ipNode.Text
xpath = "//IPSubnets/IPSubnet[position() = count(//IPAddresses/IPAddress[.='" & ipNode.Text & "']/preceding-sibling::IPAddress)+1]"
WScript.Echo "subnet", ipNode.SelectSingleNode(xpath).Text
Next
输出:
ipNode 1.1.1.1
subnet 255.0.0.0
ipNode 2.2.2.2
subnet 255.255.0.0
ipNode 3.3.3.3
subnet 255.255.255.0
谢谢你。
我看到了我的错误。直到你指出它,上下文节点才发生移动,我用一个真正的调试器将它扔到C#中,以查看节点
我试图避免进行代码更改,因为在数据集中的其他实例中使用了相同的技术
因此,由于我必须做出改变,我将采用稍微不同的路线:
ipXPath="//IPAddresses/IPAddress"
subnetXPath="//IPSubnets/IPSubnet[ $position()$ ]"
i=0
For Each ipNode In adaptersDoc.selectNodes(ipXPath)
i=i+1
WScript.Echo "ipNode", ipNode.Text
WScript.Echo "subnet", ipNode.selectSingleNode( Replace(subnetXPath,"$position()$",CStr(i)) ).Text
Next
它做了我需要做的事情。谢谢你的帮助