开发者

filter out nodes with certain id expression using xpath

Currently I've got following example xpaths of the nodes I want:

/html
 /body
  /div[@id='wp']
   /div[@id='ct']
    /div
     /div[@id='threadlist']
      /div[2]
       /form
        /table
开发者_如何学Python         /tbody[@id='normalthread_1174131']
          /tr
           /th
            /a

And something I don't need:

/html
 /body
  /div[@id='wp']
   /div[@id='ct']
    /div
     /div[@id='threadlist']
      /div[2]
       /form
        /table
         /tbody[@id='stickthread_1174132']
          /tr
           /th
            /a

Of course, I could select them all using:

/html/body/div[@id='wp']/div[@id='ct']/div/div[@id='threadlist']
                                           /div[2]/form/table/tbody[@id]/tr/th/a

But I only want to select nodes with id normalthread_xxx. As each node has different id. In other words, the target page may have following nodes:

/html/body/div[@id='wp']
           /div[@id='ct']
            /div/div[@id='threadlist']
                 /div[2]/form/table/tbody[@id='normalthread_1174133']
                                    /tr/th/a

/html/body/div[@id='wp']
           /div[@id='ct']
            /div/div[@id='threadlist']
                 /div[2]/form/table/tbody[@id='normalthread_1174134']
                                    /tr/th/a

Can we have something similar to

/html/body/div[@id='wp']
           /div[@id='ct']
            /div/div[@id='threadlist']
                 /div[2]/form/table/tbody[@id='normalthread_XXX']
                                    /tr/th/a

, in order to filter all the nodes here?


You should be able to use starts-with() (part of XPath 1.0):

/html/body/(...)/form/table/tbody[starts-with(@id,'normalthread_')]/tr/th/a
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜