Kamil
Kamil

Reputation: 1538

How to convert Xpath to CSS

My xpath is: /html/body/div/table/tbody/tr[2]/td[4]

I need to get an CSS to use it in jsoup selector.

I found a comparison between xpath and css: here, and it's said in their example (Second <E> element anywhere on page) that I can't do it. Xpath xpath=(//E)[2] CSS N\A.

Maybe I can't find what I'm looking for. Any ideas?

Here's the html I'm trying to parse (I need to get values: 1 and 3):

<div class=tablecont>
    <table width=100%>
        <tr>
            <td class=header align=center>Panel Color</td>
            <td class=header align=center>Locked</td>
            <td class=header align=center>Unqualified</td>
            <td class=header align=center>Qualified</td>
            <td class=header align=center>Finished</td>
            <td class=header align=center>TOTAL</td>
        </tr>
        <tr>
            <td align=center>
                <div class=packagecode>ONE</div>
                <div>
                <div class=packagecolor style=background-color:#FC0;></div>
                </div>
            </td>
            <td align=center>0</td>
            <td align=center>0</td>
            <td align=center>1</td>
            <td align=center>12</td>
            <td align=center class=rowhead>53</td>
        </tr>
        <tr>
            <td align=center>
                <div class=packagecode>two</div>
                <div>
                    <div class=packagecolor style=background-color:#C3F;></div>
                </div>
            </td>
            <td align=center>0</td>
            <td align=center>0</td>
            <td align=center>3</td>
            <td align=center>42</td>
            <td align=center class=rowhead>26</td>
        </tr>
    </table>
</div>

Upvotes: 5

Views: 20502

Answers (4)

New Guy
New Guy

Reputation: 393

One should learn how to write css selectors, but a for a quick fix, try: cssify

For example, I put in your xpath and it spit out: html > body > div > table > tbody > tr:nth-of-type(2) > td:nth-of-type(4)

Try it out.

Upvotes: -1

Oleksandr Knyga
Oleksandr Knyga

Reputation: 633

Works good for me.

//Author: Oleksandr Knyga
function xPathToCss(xpath) {
    return xpath
        .replace(/\[(\d+?)\]/g, function(s,m1){ return '['+(m1-1)+']'; })
        .replace(/\/{2}/g, '')
        .replace(/\/+/g, ' > ')
        .replace(/@/g, '')
        .replace(/\[(\d+)\]/g, ':eq($1)')
        .replace(/^\s+/, '');
}

Upvotes: 10

Steve Wellens
Steve Wellens

Reputation: 20620

Are you looking for something like this:

http://jsfiddle.net/YZu8D/

.tablecont tr:nth-child(2) td:nth-child(4) {background-color: yellow; }
.tablecont tr:nth-child(3) td:nth-child(4) {background-color: yellow; }

Upvotes: 0

nwellnhof
nwellnhof

Reputation: 33618

While an expression like (//E)[2] can't be represented with a CSS selector, an expression like E[2] can be emulated using the :nth-of-type() pseudo-class:

html > body > div > table > tbody > tr:nth-of-type(2) > td:nth-of-type(4)

Upvotes: 12

Related Questions