0

I'd like to target in XPath (to be used in xmlImport for Google Spreadsheets) the element that in CSS would be .forum-table tr:nth-of-type(1) .forum-number-topics:

<table class="forum-table forum-table-forums">
    <thead class="forum-header">
        <tr>
            <th class="forum-icon">&nbsp;</th>
            <th class="forum-name">Forum</th>
            <th class="forum-topics">Topics</th>
            <th class="forum-posts">Posts</th>
            <th class="forum-last-post">Last post</th>
        </tr>
    </thead>
    <tbody id="forum-table-12892-content">
        <tr class="forum-row even container-12897-child" id="forum-12897">
            <td class="forum-list-icon forum-list-icon-default"> <span class="forum-list-icon-wrapper"><span>No new</span></span>
            </td>
            <td colspan="1" class="forum-details">
                <div class="forum-name"> <a href="/forums/settimana-1-1">Settimana 1</a>

                </div>
            </td>
            <td class="forum-number-topics">
                <div class="forum-number-topics">20</div>
            </td>
            <td class="forum-number-posts">171</td>
            <td class="forum-last-reply">n/a</td>
        </tr>
        <tr class="forum-row odd container-13043-child" id="forum-13043">
            <td class="forum-list-icon forum-list-icon-default"> <span class="forum-list-icon-wrapper"><span>No new</span></span>
            </td>
            <td colspan="1" class="forum-details">
                <div class="forum-name"> <a href="/forums/settimana-2-0">Settimana 2</a>

                </div>
            </td>
            <td class="forum-number-topics">
                <div class="forum-number-topics">21</div>
            </td>
            <td class="forum-number-posts">143</td>
            <td class="forum-last-reply">n/a</td>
        </tr>
        <tr class="forum-row even container-13107-child" id="forum-13107">
            <td class="forum-list-icon forum-list-icon-default"> <span class="forum-list-icon-wrapper"><span>No new</span></span>
            </td>
            <td colspan="1" class="forum-details">
                <div class="forum-name"> <a href="/forums/settimana-3-0">Settimana 3</a>

                </div>
            </td>
            <td class="forum-number-topics">
                <div class="forum-number-topics">20</div>
            </td>
            <td class="forum-number-posts">91</td>
            <td class="forum-last-reply">n/a</td>
        </tr>
    </tbody>
</table>

I specified tr:nth-of-type(1) because I need the same syntax in Xpath (with numeric entity).

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
MultiformeIngegno
  • 6,959
  • 15
  • 60
  • 119
  • When you say "the element that in CSS would be `.forum-table tr:nth-of-type(1) .forum-number-topics`", there are multiple elements that would match that CSS, right? So for example, do you the XPath to select `` elements, or `
    ` elements, or both?
    – LarsH Jun 02 '15 at 14:42

1 Answers1

0

As a literal translation, I would try

//*[contains(@class, 'forum-table')]//tr[1]//*[contains(@class, 'forum-number-topics')]

Note that this will select both <td> and <div> elements, e.g. where you have

<td class="forum-number-topics">
    <div class="forum-number-topics">

in the input. Also note that this assumes you don't have "ornery" classes that contain forum-table but not as a whole class name, e.g. if your first table element had class="forum-table-forums" without forum-table in there. If you need to be more rigorous about matching class names, you can do

//*[contains(concat(' ', @class, ' '), ' forum-table ')]//tr[1]//
  *[contains(concat(' ', @class, ' '), ' forum-number-topics ')]

(line broken for clarity).

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • To answer the part of your question about the XPath equivalent for `tr:nth-of-type(1)`: in XPath, `tr[n]` selects any `tr` element that is the nth `tr` child of its parent element. In particular, `...//tr[1]` can select several `tr` elements, each of which is the first `tr` child of its respective parent; not just the first `tr` descendant (of whatever `...` was). – LarsH Jun 02 '15 at 15:00
  • P.S. If Google's ImportXML supports XPath 2.0, and I don't know if it does, you can use `[tokenize(@class,'\s+')='forum-table']` instead of `[contains(...)`]. See http://stackoverflow.com/a/12165195/423105 – LarsH Jun 02 '15 at 15:08