1

I have an Xpath like following:

"//<path to some table>/*/td[1]/text()"

and it returns text values of all non-empty tds, for example:

<text1>, <text2>, <text3>

But the problem is that between nodes, that contain mentioned values could be some empty tds elements: What i want is to get result that contain some identifiers, that there is those empty values, for example:

<text1>,<>, <>, <text2>, <text3>, <>

or

<text1>,<null>, <null>, <text2>, <text3>, <null>

I tried to use next one:

"//<path to some table>/*/string(td[1]/text())"

but it returns undefined

Of course, I could just get whole node and then work with it in my code (cut all unnecessary info), but may be there is a better way?

html example for that case:

<html>
<body>
<table class="tablesorter">
<tbody>     
    <tr class="tr_class">
                    <td>text1</td>
                    <td>{some text}</td>                    

    </tr>

    <tr class="tr_class">
                    <td></td>
                    <td>{some text}</td>   
    </tr>

    <tr class="tr_class">
                    <td>text2</td>
                    <td>{some text}</td>                    
    </tr> 

    <tr class="tr_class">
                    <td>text3</td>
                    <td>{some text}</td>                    
    </tr> 

    <tr class="tr_class">
                    <td></td>
                    <td>{some text}</td>                    
    </tr>   

</tbody>
</table>
</body>
</html>
1
  • post your actual XML, not some made-up text that doesn't make sense and we may be able to help Commented Apr 12, 2013 at 14:28

2 Answers 2

1

Well simply select the td elements, not its text() child nodes. So with the path changed to //<path to some table>/*/td[1] or maybe //<path to some table>/*/td you will get a node-set of td elements, whether they are empty or not, and you can then access the string contents of each node (with XPath (select string(.) for each element node) or host environment method e.g. textContent in the W3C DOM or text in the MSXML DOM.). That way the empty strings will be included.

In case you use XPath 2.0 or XQuery you can directly select //<path to some table>/*/td/string(.) to have a sequence of string values. But that approach with a function call in the last step is not supported in XPath 1.0, there you can select the td element nodes and then access the string value of each in a separate step.

Sign up to request clarification or add additional context in comments.

5 Comments

just checked $x("//table [@class='tablesorter']/tbody/*/td[1]/string(.)") and it returns an Xpath error both in devTools and Firebug
Well I clearly stated that //<path to some table>/*/td/string(.) is XPath 2.0 only so trying that in Firebug is of course not meaningful and fails. But if you are using XPath 1.0 within Mozilla browsers then you can access all the td element nodes with //table [@class='tablesorter']/tbody/*/td[1] and then access the textContent property of those element nodes in the DOM to get the string contents.
See jsfiddle.net/9SyxE how to use XPath 1.0 and Mozilla to find td element nodes, including empty ones, and output its textContent.
And see jsfiddle.net/e6wtk/1 if you want to do it all with XPath 1.0. Instead of reading out the textContent that example uses the XPath API to read out the string value of each td element selected in the outer query.
looks like jsfiddle.net/e6wtk/1 is the right answer. I'm using resembling approach for creating Xpath extension point in testing framework.
0

Do you mean you want only the td[1] with text and get rid of ones without text? If so, you can use this xpath

//td[1][string-length(text()) > 1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.