e-anima
e-anima

Reputation: 43

Xpath nested tables

I have a Table, see Code. Its a table that has a table in it, so its nested. Now i want to get all vales of the parent table only and then all values of the child table.

To get the childs data i can do this:

$query = '//*[@id="WordClass"]/table[2]/tr/td[2]/table/tr';

$nodes = $xpath->query($query);

foreach ($nodes as $node) { //do more querys to get the td data and save it..

My problem is how to only get the data of the parent table without getting the child data/tr/td also.

<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr valign="top">
    <td>
        <table cellpadding="1" cellspacing="2" border="0">
            <tr>
                <td class="colTitle" align="center" colspan="4">
                    Da Titel
                </td>
            </tr>
            <tr>
                <td class="colTitle" align="center" colspan="2">One
                </td>
                <td class="colTitle" align="center" colspan="2">Two
                    I
                </td>
            </tr>
            <tr>
                <td class="colSubTitle">Pe</td>
                <td class="colSubTitle">Ve</td>
                <td class="colSubTitle">Pe</td>
                <td class="colSubTitle">Ve</td>
            </tr>
            <tr>
                <td class="rowTitle">x</td>
                <td class="colVerbDef">y</td>
                <td class="rowTitle">z</td>
                <td class="colVerbDef">c</td>
            </tr>
            <tr>
                <td class="rowTitle">r</td>
                <td class="colVerbDef">t</td>
                <td class="rowTitle">z</td>
                <td class="colVerbDef">z</td>
            </tr>

        </table>
    </td>
    <td>
        <table cellpadding="1" cellspacing="2" border="0">
            <tr>
                <td class="colTitle" align="center" colspan="4">
                    Da Titel2
                </td>
            </tr>
            <tr>
                <td class="colTitle" align="center" colspan="2">One
                </td>
                <td class="colTitle" align="center" colspan="2">Two
                    I
                </td>
            </tr>
            <tr>
                <td class="colSubTitle">Pe2</td>
                <td class="colSubTitle">Ve2</td>
                <td class="colSubTitle">Pe2</td>
                <td class="colSubTitle">Ve2</td>
            </tr>
            <tr>
                <td class="rowTitle">x2</td>
                <td class="colVerbDef">y2</td>
                <td class="rowTitle">z2</td>
                <td class="colVerbDef">c2</td>
            </tr>
            <tr>
                <td class="rowTitle">r2</td>
                <td class="colVerbDef">t2</td>
                <td class="rowTitle">z2</td>
                <td class="colVerbDef">z2</td>
            </tr>

        </table>
    </td>
</tr>
</tbody>

Upvotes: 1

Views: 1358

Answers (1)

scrowler
scrowler

Reputation: 24425

You can get the contents of the parent table's td elements using a direct path from the root:

/table/tbody/tr/td

The contents of those cells happen to be another table element, but you can strip those out with DOMDocument.

To get the inner tables' td elements only excluding the parents, you can look for tables that have a td parent, then select its tds:

//td/table//td

If I've misunderstood your question, please feel free to explain further and I will update.

Upvotes: 1

Related Questions