Question

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.

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top