Ruby Nokogiri::Slop获取节点属性时出现问题?

Ruby Nokogiri::Slop获取节点属性时出现问题?,ruby,nokogiri,Ruby,Nokogiri,我有一个SVG文件,其中包含: <text x="10" y="20" style="font-family: Helvetica; font-size : 24; fill : #ff0000; stroke : #000000;">SVG text styling</text> <text x="85" y="150" style="font-family: Helvetic

我有一个SVG文件,其中包含:

<text x="10"  y="20"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">SVG text styling</text>
<text x="85"  y="150"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">This is the second piece of text</text>
<text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">This is the third piece of text</text>

         <text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">test text</text>
         <text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">data data</text>
         <text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">txt txt txt</text>
text_styles 
# => [{"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"}]
而本局:

<% @test.xpath('//text').map do |i|%>
<%=i%>
<%  end  %>

但是它不起作用。

为了在ERB中使用数据,您应该首先将其转换为控制器中更友好的数据。此代码将生成一个可以在视图中剥离的哈希数组:

require 'nokogiri'

doc = Nokogiri::XML(<<EOT)
<svg>
<text x="10"  y="20"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">SVG text styling</text>
<text x="85"  y="150"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">This is the second piece of text</text>
<text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">This is the third piece of text</text>

         <text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">test text</text>
         <text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">data data</text>
         <text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">txt txt txt</text>
</svg>
EOT
运行时,将返回包含以下内容的
text\u样式

<text x="10"  y="20"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">SVG text styling</text>
<text x="85"  y="150"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">This is the second piece of text</text>
<text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">This is the third piece of text</text>

         <text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">test text</text>
         <text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">data data</text>
         <text x="45"  y="250"
  style="font-family: Helvetica;
         font-size  : 24;
         fill       : #ff0000;
         stroke     : #000000;">txt txt txt</text>
text_styles 
# => [{"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"},
#     {"font-family"=>"Helvetica",
#      "font-size"=>"24",
#      "fill"=>"#ff0000",
#      "stroke"=>"#000000"}]
代码读取数据并将其解析为XML。然后,使用
search
查找CSS选择器
text
。为什么是CSS?它更容易阅读

一旦找到
节点,它就会从该节点检索
style
属性,然后在
上拆分其值
上拆分它们,并去除前导/尾随空格


最后,它将生成的数组数组转换为散列并返回它。

XML中的
字体系列
字体大小
填充
笔划
项不是XML属性,因此无法(直接)使用Nokogiri获取它们。它们是字符串的一部分,该字符串是
style
属性的值。您必须获取该字符串,然后以某种方式在Ruby中解析它

下面的示例显示了如何获取第一个
text
元素的这四个值:

# first get the complete string:
styles = @test.at_xpath("//text/@style").value

# next split the string into key values pairs on ;, then each pair
# into spearate strings on :, and create a hash with the result
style_hash = Hash[style1.split(/\s*;\s*/).map { |s| s.split(/\s*:\s*/)}]

定义“它不起作用”。当你运行它时会发生什么?你有例外吗?这个例外说明了什么?它是否运行但不生成所需的输出?如果是这样的话,展示这个输出并解释它的错误。我现在知道这个概念,它对我来说很好,但是我如何获得Xpath呢lenght@AndroidMan我不明白你的意思。你是说如何获取所有
text/@style
节点?请注意,我使用了
at_path
来简化示例,它只返回第一个节点–使用
xpath
获取tham all。看一看,他包含了一个提取所有节点的类似示例。
# first get the complete string:
styles = @test.at_xpath("//text/@style").value

# next split the string into key values pairs on ;, then each pair
# into spearate strings on :, and create a hash with the result
style_hash = Hash[style1.split(/\s*;\s*/).map { |s| s.split(/\s*:\s*/)}]