2

Listing a sample of structure:

    <tbody class="">
                <tr class="">
                    <td class=""><img src="http://dummyurl1.jpg" class=""></td>
                    <td class="">Test</td>
                    <td class="">Test-0000001</td>
                    <td class="">
                        <button data-toggle="modal" data-target="#AddEditDialog" class="btn btn-primary" data-ember-action="" data-ember-action-546="546">
                            Edit Tests
                        </button>
                        <button class="btn btn-danger" data-ember-action="" data-ember-action-547="547">
                            Remove Tests
                        </button>
                        <button class="btn btn-default" data-ember-action="" data-ember-action-548="548">Print Test</button>
                    </td>
                </tr>
                <tr class="">
                    <td class=""><img src="http://dummyurl.jpg" class=""></td>
                    <td class="">Testing</td>
                    <td class="">Test-0000002</td>
                    <td class="">
                        <button data-toggle="modal" data-target="#AddEditDialog" class="btn btn-primary" data-ember-action="" data-ember-action-549="549">
                            Edit Tests
                        </button>
                        <button class="btn btn-danger" data-ember-action="" data-ember-action-550="550">
                            Remove Tests
                        </button>
                        <button class="btn btn-default" data-ember-action="" data-ember-action-551="551">Print Test</button>
                    </td>
                </tr>
            </tbody>

Here I do have the Test Id's as Test-000000X. However, I tried and could not isolate the elements at row level.

3 Answers3

0

"Here I do have the Test Id's as Test-000000X. However, I tried and could not isolate the elements at row level."

How about :

//tr[td='Test-000000X']

The above should return the row where one of the column content equals 'Test-000000X'.

har07
  • 88,338
  • 12
  • 84
  • 137
0

For single access use

//tr[1]/td[3]/text()
//tr[2]/td[3]/text()

Example

[~] 
$ xmllint --xpath "//tr[1]/td[3]/text()"  yourXmlFile.xml 
Test-0000001

[~] 
$ xmllint --xpath "//tr[2]/td[3]/text()"  yourXmlFile.xml
Test-0000002

For automatically iterating over all occurrences use

   //tr/td[3]/text()

Example with xmllint

$ xmllint --xpath "//tr/td[3]/text()"  yourXmlFile.xml

Gives

Test-0000001Test-0000002

Example with newlines in between (using this trick)

xmllint --shell yourXmlFile.xml <<< 'cat //tr/td[3]/text()'

gives

/ >  -------
Test-0000001
 -------
Test-0000002

With yourXmlFile.xml

<tbody class="">
            <tr class="">
                <td class=""><img src="http://dummyurl1.jpg" class=""/></td>
                <td class="">Test</td>
                <td class="">Test-0000001</td>
                <td class="">
                    <button data-toggle="modal" data-target="#AddEditDialog" class="btn btn-primary" data-ember-action="" data-ember-action-546="546">
                        Edit Tests
                    </button>
                    <button class="btn btn-danger" data-ember-action="" data-ember-action-547="547">
                        Remove Tests
                    </button>
                    <button class="btn btn-default" data-ember-action="" data-ember-action-548="548">Print Test</button>
                </td>
            </tr>
            <tr class="">
                <td class=""><img src="http://dummyurl.jpg" class=""/></td>
                <td class="">Testing</td>
                <td class="">Test-0000002</td>
                <td class="">
                    <button data-toggle="modal" data-target="#AddEditDialog" class="btn btn-primary" data-ember-action="" data-ember-action-549="549">
                        Edit Tests
                    </button>
                    <button class="btn btn-danger" data-ember-action="" data-ember-action-550="550">
                        Remove Tests
                    </button>
                    <button class="btn btn-default" data-ember-action="" data-ember-action-551="551">Print Test</button>
                </td>
            </tr>
        </tbody>
Community
  • 1
  • 1
jschnasse
  • 8,526
  • 6
  • 32
  • 72
  • Thank you, but my problem is to isolate the elements at every row. This way I will again get Test-000000 and Test-0000002. Whereas I just need one at a time. – Mayank Sargoch Mar 16 '17 at 09:59
  • Using of xmllint is just an example. In other contexts you will get a list of isolated matches, e.g. in Java a NodeList or similar. So the xpath itself should do what you have asked for. I will add some trick on how to separate the strings in bash. – jschnasse Mar 16 '17 at 10:16
0

Try to use below XPath:

//td[text()="Test-0000001"]/following-sibling::td/button[.="Edit Tests"]

for Test-0000001 Edit Tests button and

//td[text()="Test-0000001"]/following-sibling::td/button[.="Remove Tests"]

for Test-0000001 Remove Tests button

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Hello, Thank you for the response. I have never used following-sibling syntax before but it works like a charm. Even making the xpath relative is quite easier with this. I appreciate the help. – Mayank Sargoch Mar 16 '17 at 10:43
  • You can mark this answer as `Accepted` if it solved your issue – Andersson Mar 16 '17 at 10:44