-

List of all td nodes with empty content

I am using java 1.6. I have xml strcuture like below.

- <table column="6" row="22">
- <tr>
  <td bold="true" class="header" font="Arial Narrow" size="9">Jinesh</td> 
  <td align="Right" bold="true" font="Arial Narrow" size="9">2007</td> 
  <td align="Right" bold="true" font="Arial Narrow" size="9">2008</td> 
  <td align="Right" bold="true" font="Arial Narrow" size="9">2009E</td> 
  <td align="Right" bold="true" font="Arial Narrow" size="9">2010E</td> 
  <td align="Right" bold="true" font="Arial Narrow" size="9">2011E</td> 
  </tr>
<tr>
  <td font="Arial Narrow" size="9" /> 
  <td font="Arial Narrow" size="9" /> 
  <td font="Arial Narrow" size="9" /> 
  <td font="Arial Narrow" size="9" /> 
  <td font="Arial Narrow" size="9" /> 
  <td font="Arial Narrow" size="9" /> 
  </tr>

      

Now I want to list all td node with empty content, for example the second tag has no content at all. I am currently using the below Xpath expression to list all the td nodes.

//table//tr//td[@font='Arial Narrow' and @size='9']

      

But the above expression lists all the td nodes whether it is empty or not. Can anyone help me with the xpath expression to display all td nodes with empty content?

+3


source to share


1 answer


You can search for empty nodes by testing the empty value as follows. Optionally, you can add normalize-space()

to strip away all encountered spaces:

//td[normalize-space(.)='']

      

Edit



Re: If I want to list td nodes if all children of td node parent tr node is empty

//table/tr[not(td[normalize-space(.)!=''])]/td

      

Basically, find tr where there are no non-empty child td's and then navigate to the child td. There is no need to apply an empty filter to td

again, as we have already confirmed this in the level tr

. It would also be possible to test this in a "positive" way by counting an empty td and all td and testing, but I feel that negative is easier (namely without empty td)

+4


source







All Articles