具有MSXML的XPath,“范围" XPath表达式 [英] XPath with MSXML, "scope" of XPath-expressions

查看:135
本文介绍了具有MSXML的XPath,“范围" XPath表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用带有XPath表达式的Microsoft XML Core Services 6.0(MSXML)时遇到了一个理解问题. 我将问题分解为最简单的情况.因此,让我们采用以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <element name="E1A1">
        <subEle value="1a"/>
        <subEle value="1b"/>
        <subEle value="1c"/>
    </element>

    <element name="E2A1">
        <subEle value="2a"/>
        <subEle value="2b"/>
        <subEle value="3b"/>
    </element>

    <element name="E3A1">
        <subEle value="3a"/>
        <subEle value="3b"/>
        <subEle value="3c"/>
    </element>
</root>

我想获取每个元素"的"value"属性.我将使用伪代码来描述 我的问题,我将专注于重要的事情,所以我不会写我如何初始化 Msxml2.DOMDocument变量等. 首先,我得到所有具有name属性的"element"节点:

oNodeList = oDom.selectNodes("//element[@name]")

selectNodes语句的结果是一个节点列表,在这里我逐节点访问项目 在for循环中.在此循环中,我执行了另一个selectNodes语句,该语句给了我(至少我这样认为) 每个"element"的"subEle":

for i from 1 to oNodeList.length
    oNodeMain = oNodeList.nextNode()    
    oNodeResList = oNodeMain.selectNodes("//subEle")
    msgInfo("n items", oNodeResList.length)
endFor

问题来了:循环中的selectNodes语句似乎具有 ALL "subEle" 在适用范围;消息框弹出三下,告诉我节点列表的长度为9. 我本来希望它弹出3次,每次告诉我节点列表的长度为3(因为 每个"element"正好有3个"subEle"),因为我是在"oNodeMain"上执行selectNodes语句, 在每个循环中获取nextNode.也许我只需要在循环中修改XPath-expression即可 不要使用"//",因为这样可以正常工作,但是我不知道为什么.

我为此使用的程序是Paradox 11,并且我使用OLE的MSXML. 这种行为是正常的"吗,我的误解在哪里?关于如何实现自己的任何建议 欢迎尝试.

解决方案

请勿使用以/开头的绝对路径,而应使用相对路径,即oNodeMain.selectNodes("subEle")选择oNodeMain的所有subEle子元素和oNodeMain.selectNodes(".//subEle")选择oNodeMain的所有后代subEle元素.

//开头的路径是从根节点(也称为文档节点)搜索的.

i have an understanding-problem using Microsoft XML Core Services 6.0 (MSXML) with XPath-expressions. I´ve broken down the problem to the most simple case. So let´s take the following XML-File:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <element name="E1A1">
        <subEle value="1a"/>
        <subEle value="1b"/>
        <subEle value="1c"/>
    </element>

    <element name="E2A1">
        <subEle value="2a"/>
        <subEle value="2b"/>
        <subEle value="3b"/>
    </element>

    <element name="E3A1">
        <subEle value="3a"/>
        <subEle value="3b"/>
        <subEle value="3c"/>
    </element>
</root>

I want to get the "value"-attribues per "element". I will use pseudo-code to describe my problem and i will focus on the important things, so i will not write how i initialize the Msxml2.DOMDocument variable etc. . First, i get all "element"-nodes that have a name-attribute:

oNodeList = oDom.selectNodes("//element[@name]")

The result of the selectNodes-statement is a nodelist, where i access the items node by node in a for-loop. In this loop, i execute another selectNodes-statement, that gives me (at least i thought so) the "subEle"s for each "element":

for i from 1 to oNodeList.length
    oNodeMain = oNodeList.nextNode()    
    oNodeResList = oNodeMain.selectNodes("//subEle")
    msgInfo("n items", oNodeResList.length)
endFor

And here comes the problem: the selectNodes-statement in the loops seems to have ALL "subEle"s in scope; the messagebox pops up three times, telling me the length of the nodelist is 9. I would have expected that it pops up 3 times, telling me each time that the nodelist has a length of 3 (because every "element" has exactly 3 "subEle"s), since i´m doing the selectNodes-statement on "oNodeMain", which gets the nextNode in each loop. Maybe i just need to modify XPath-expression in the loop and don´t use the "//", because it works then, but i have no idea why.

The program i use for this is Paradox 11 and i use MSXML by OLE. Is this behaviour "normal", where is my misunderstanding? Any suggestions on how to achieve what i´m trying are welcome.

解决方案

Don't use an absolute path starting with /, instead use a relative path i.e. oNodeMain.selectNodes("subEle") selects all subEle child elements of oNodeMain and oNodeMain.selectNodes(".//subEle") selects all descendant subEle elements of oNodeMain.

Your path starting with // searches from the root node (also called document node).

这篇关于具有MSXML的XPath,“范围" XPath表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆