2016-09-16 1 views
-1

Iは、次の形式を持つテーブルを持っている:さまざまな行を取得するための適切なnokogiri xpathは何ですか?

<tr class="style6"><td>SomeStuff</td></tr> 
<tr><td>Some other stuff</td></tr> 
<tr><td>Some other stuff</td></tr> 
<tr><td>Some other stuff</td></tr> 
<tr><td>Some other stuff</td></tr> 
<tr><td>Some other stuff</td></tr> 
<tr class="style6"><td>SomeStuff</td></tr> 
<tr><td>Some other stuff</td></tr> 
<tr><td>Some other stuff</td></tr> 
<tr><td>Some other stuff</td></tr> 
<tr><td>Some other stuff</td></tr> 
<tr><td>Some other stuff</td></tr> 

私は反復処理できるグループに分割行(次style6発生前の最後の行にstyle6クラスから始まる)のブロックが欲しいです。これをブロックに分割する方法はありますか?私はXpath positionの機能を認識していますが、この文脈では意味があるとは確信していません。

アイデア?

答えて

-1

有用なパターンは、前に来た<tr class="style6"><td>SomeStuff</td></tr>を数えることです。あなたの例では最初のグループのために

、それは次のようになります。第二グループのために

//tr[not(@class="style6")][count(preceding-sibling::tr[@class="style6"])=1]

//tr[not(@class="style6")][count(preceding-sibling::tr[@class="style6"])=2]

など

私が使用していない

nokogiriので、ここではPythonとlxmlを使用して例:

>>> import lxml.html 
>>> from pprint import pprint 

>>> doc = lxml.html.fromstring('''<tr class="style6"><td>SomeStuff</td></tr> 
... <tr><td>Some other stuff group 1</td></tr> 
... <tr><td>Some other stuff group 1</td></tr> 
... <tr><td>Some other stuff group 1</td></tr> 
... <tr><td>Some other stuff group 1</td></tr> 
... <tr><td>Some other stuff group 1</td></tr> 
... <tr class="style6"><td>SomeStuff</td></tr> 
... <tr><td>Some other stuff group 2</td></tr> 
... <tr><td>Some other stuff group 2</td></tr> 
... <tr><td>Some other stuff group 2</td></tr> 
... <tr><td>Some other stuff group 2</td></tr> 
... <tr><td>Some other stuff group 2</td></tr> 
... <tr class="style6"><td>SomeStuff</td></tr> 
... <tr><td>Some other stuff group 3</td></tr> 
... <tr><td>Some other stuff group 3</td></tr> 
... <tr><td>Some other stuff group 3</td></tr> 
... <tr><td>Some other stuff group 3</td></tr> 
... <tr><td>Some other stuff group 3</td></tr>''') 

>>> pprint(list(lxml.html.tostring(row) 
...   for row in doc.xpath(''' 
...     //tr[not(@class="style6")] 
...      [count(preceding-sibling::tr[@class="style6"])=1]'''))) 
[b'<tr><td>Some other stuff group 1</td></tr>\n', 
b'<tr><td>Some other stuff group 1</td></tr>\n', 
b'<tr><td>Some other stuff group 1</td></tr>\n', 
b'<tr><td>Some other stuff group 1</td></tr>\n', 
b'<tr><td>Some other stuff group 1</td></tr>\n'] 
>>> pprint(list(lxml.html.tostring(row) 
...   for row in doc.xpath(''' 
...     //tr[not(@class="style6")] 
...      [count(preceding-sibling::tr[@class="style6"])=2]'''))) 
[b'<tr><td>Some other stuff group 2</td></tr>\n', 
b'<tr><td>Some other stuff group 2</td></tr>\n', 
b'<tr><td>Some other stuff group 2</td></tr>\n', 
b'<tr><td>Some other stuff group 2</td></tr>\n', 
b'<tr><td>Some other stuff group 2</td></tr>\n'] 
>>> pprint(list(lxml.html.tostring(row) 
...   for row in doc.xpath(''' 
...     //tr[not(@class="style6")] 
...      [count(preceding-sibling::tr[@class="style6"])=3]'''))) 
[b'<tr><td>Some other stuff group 3</td></tr>\n', 
b'<tr><td>Some other stuff group 3</td></tr>\n', 
b'<tr><td>Some other stuff group 3</td></tr>\n', 
b'<tr><td>Some other stuff group 3</td></tr>\n', 
b'<tr><td>Some other stuff group 3</td></tr>'] 
>>> 
関連する問題