Question

Suppose I have this HTML file :

<html>
    <table class="class1">
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
    </table>

    <table class="class2">
        <tr>
            <td>a</td>
            <td>b</td>
            <td>c</td>
            <td>d</td>
            <td>e</td>
        </tr>
    </table>

    <table class="class3">
        <tr>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </table>
</html>

I would like to query this file with XPath in order to get this result : 1234512345 with this code :

var xNavigator = xPathDoc.CreateNavigator();
var iterator = xNavigator.Select("//html/table[@class='class1']/tr/td|//html/table[@class='class3']/tr/td");
while (iterator.MoveNext())
    Console.Write(iterator.Current.InnerXml);

but the result is : 1212345345

Do you have any idea how can I get 1234512345 instead 1212345345 ?

Thank you.

Was it helpful?

Solution

Since you're talking "node-sets" I assume you're using XPath 1.0 - you really ought to say so. A node-set in XPath 1.0 is a set of nodes with no particular order; however XSLT always processes the nodes in a node-set in document order, and this has established a precedent which as far as I know all XPath 1.0 processors follow whether they are being used in XSLT or not. So expressions that return node-sets are likely to return the nodes in document order, which is the effect you are observing.

XPath 2.0 introduces sequences, which allows you to return nodes in any order of your own choosing.

OTHER TIPS

I think you're going to need to perform two Selects. As described in the discussion below, XPath 1.0 (which is the only version .NET has support for) doesn't define node order, and the implementations all return the nodes in document order.

Returning XPath correct order ( insted of document order)

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