What XPath selects odd TRs from a table, starting

2019-06-20 06:11发布

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.).

标签: dom xpath xhtml
2条回答
等我变得足够好
2楼-- · 2019-06-20 06:28
"/table/tr[position() mod 2 = 1 and position() > 1]"
查看更多
淡お忘
3楼-- · 2019-06-20 06:37

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>
查看更多
登录 后发表回答