0

私のコード:

require 'rexml/document'
require 'rexml/xpath'

doc = REXML::Document.new(response)
REXML::XPath.each(doc, "*//categoryName") { |element| puts element.text }

これが返してほしいのは、タグ内のテキスト要素です...実際に返されるのは次のとおりです。

<categoryName> ... </>

これを修正する方法について何か考えはありますか?

4

1 に答える 1

0

サンプルの例で私にとってはうまくいきます:

require 'rexml/document'
require 'rexml/xpath'

doc = REXML::Document.new("<p>some text <b>this is bold!</b> more text</p>")
REXML::XPath.each(doc, "*//b") { |element| puts element.text }
# >> this is bold!

別の例を参照してください。

require 'rexml/document'
require 'rexml/xpath'

str = <<-eot
<version>
  <locale name="en-US">
    <title>This is the Title</title>
    <description>this is the description</description>
    <keywords>
      <keyword>this</keyword>
      <keyword>that</keyword>
      <keyword>the other</keyword>
    </keywords>
  </locale>
</version>
eot
doc = REXML::Document.new(str)
REXML::XPath.each(doc, "//title") { |element| puts element.text }
REXML::XPath.each(doc, "//keyword") { |element| puts element.text }
# >> This is the Title
# >> this
# >> that
# >> the other
于 2013-06-26T20:08:18.893 に答える