2013-10-01 2 views
1

Java에서 동일한 경로에서 요소 수를 얻으려면 어떻게해야합니까? 예 :Java : 동일한 경로에서 XPath가 요소 수를 얻습니다.

이 HTML에서
<html> 
    <head> 
     <title>Title is here</title> 
    </head> 
    <body> 
     <div id="div1"> 
      <table id="table1"> 
       <tr> 
        <td>1</td> 
        <td>2</td> 
        <td>3</td> 
       </tr> 
       <tr> 
        <td>11</td> 
        <td>22</td> 
        <td>33</td> 
       </tr> 
      </table> 
      <table id="table2"> 
       <tr> 
        <td>111</td> 
        <td>222</td> 
        <td>333</td> 
       </tr> 
       <tr> 
        <td>1111</td> 
        <td>2222</td> 
        <td>3333</td> 
       </tr> 
      </table> 
     </div> 
     <div id="div2"> 
      <table id="table3"> 
       <tr> 
        <td>11111</td> 
       </tr> 
      </table> 
     </div> 
    </body> 
</html> 

,

  • /html/head/title -> 반환 33
  • /html/body/div[2]/table/tr/td - ->이

11111을 반환하지만 N있다>는 "Title is here"

  • /html/body/div[1]/table[1]/tr[2]/td[3] 반환 td가 tr, 또는 어떤 테이블에 m tr가 있거나 문서에 많은 테이블이 있습니다.

    count와 같은 요소 수를 제공하는식이 있습니까 ("/ html/body/div [1]/table [1]/tr [2] ") 그리고 3을 리턴 할 것인가? 이 방법이 없으면 Java에서 내 자신의 도우미 메서드를 작성할 수 있습니까?

  • +0

    'XPath' 표현식이'NodeList' 나'size()','getLength()'등등을 호출 할 수있는 종류의 것을 반환하지 않습니까? –

    +0

    나는 똑같은 것을 정말로 궁금해합니다. 나는 수색하고 어떤 길이나 연습도 찾지 못했다. – OguzOzkeroglu

    +1

    xpath 자체의'count()'함수를 사용하지 않는 이유는 무엇입니까? –

    답변

    1

    이 XSLT :

    <?xml version="1.0" encoding="ISO-8859-1"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
        <xsl:template match="/"> 
        <xsl:message>All of the td's: <xsl:value-of select="count(//td)"/> </xsl:message> 
        <xsl:message>All of the td's in table1: <xsl:value-of select="count(//table[@id='table1']//td)"/></xsl:message> 
        </xsl:template> 
    </xsl:stylesheet> 
    

    이 출력을 생성

    [XSLT 상기 TD의 모든 13

    [XSLT 상기 TD의 표에서의 모든 : 6

    +0

    감사합니다. 전에'count() '를 보지 못했습니다. – OguzOzkeroglu

    관련 문제