user1593846
user1593846

Reputation: 756

Is it possible to use a xpath within a xpath?

I have a table that looks like this:

<table>     
    <thead>
        <tr>
          <th data-th-key="col1"> <span> Foo </span>  </th>
          <th data-th-key="col2"> <span> Bar </span>  </th>
        </tr>
    </thead>
        <tbody>
            <tr>   
        <td>
                <div>
              <input data-model-key="col1">    
            </div>
        </td>
        <td>
            <div>
              <input data-model-key="col2">    
            </div>
        </td>
      </tr>
        </tbody>
</table>    

To find the right input element I have to know the data-th-key from the table head. Is there any way to use the first xpath inside the second one?

Xpath one:

//table//thead//span[translate(normalize-space(.),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='foo']/parent::th/@data-th-key"

Xpath two:

//table//tbody//tr//td//div//input[@data-model-key='col1']

So I want to replace the col1 value in the second one with Xpath one.

Upvotes: 1

Views: 63

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

You can write an expression like

//table//tbody//tr//td//div//input[@data-model-key = //table//thead//span[translate(normalize-space(.),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='foo']/parent::th/@data-th-key]

yes, that is possible and meaningful and selects the input (or the inputs) whose data-model-key is equal to that data-th-key attribute.

Upvotes: 3

Related Questions