2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

xpath1.0で ends-with function を使いたい時

Posted at

xpath1.0ではstarts-with関数は用意されてるんですが、ends-withxpath2.0からの実装になっています。

残念ながらNokogiriは xpath 1.0 しかサポートしてないので ends-with が使えません。

代わりのやり方

例えばidregisterで終わる要素を取得するには

.rb
doc.xpath("//*[substring(@id, string-length(@id) - string-length('register') +1) = 'register']")

としましょう。

ダメな例

「特定の文字列の後が空白なら末尾の文字列」と確定できるかと思ったんですが、文字列にマッチしないとすべて返ってきてしまいます。

.rb
doc.xpath("//*[substring-after(@id, 'register`) = '']")

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?