What XPath selects odd TRs from a table, starting with the third?

别等时光非礼了梦想. 提交于 2019-12-10 13:23:42

问题


I have a table:

<table>  
    <tr><td>1</td></tr>  
    <tr><td>2</td></tr>  
    <tr><td>3</td></tr>  
    <tr><td>4</td></tr>  
    <tr><td>5</td></tr>  
    <tr><td>6</td></tr>  
    <tr><td>7</td></tr>  
    <tr><td>8</td></tr>  
    <tr><td>9</td></tr>  
</table>

I need an XPath to select odd rows, starting on the third row (3, 5, 7, 9, etc.).


回答1:


I think 'position()' function of XPATH will do the job. Returns the index position of the node that is currently being processed. you need to do position() mod 2.

Here is XSLT solution

<xsl:for-each select="tr">
  <xsl:choose>
   <xsl:when test="position() mod 2 = 1 and position() > 1">
      ...do smthng ....
   </xsl:when>
   <xsl:otherwise>...do something else...</xsl:otherwise>
  </xsl:choose>
</xsl:foreach>



回答2:


"/table/tr[position() mod 2 = 1 and position() > 1]"


来源:https://stackoverflow.com/questions/1081394/what-xpath-selects-odd-trs-from-a-table-starting-with-the-third

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!