使用nokogiri builder (ruby)设置标记属性并向标记添加纯文本内容

时间:2022-11-19 14:48:28

I am trying to build XML using Nokogiri with some tags that have both attributes and plain text inside the tag. So I am trying to get to this:

我正在尝试使用Nokogiri构建XML,其中一些标记包含了标签内的属性和纯文本。所以我想说

<?xml version="1.0"?>
<Transaction requestName="OrderRequest">
  <Option b="hive">hello</Option>
</Transaction>

Using builder I have this:

使用builder时,我有:

builder = Nokogiri::XML::Builder.new { |xml|
  xml.Transaction("requestName" => "OrderRequest") do
    xml.Option("b" => "hive").text("hello")
  end
}

which renders to:

它呈现:

<Transaction requestName="OrderRequest">
  <Option b="hive" class="text">hello</Option>
</Transaction>

So it produces <Option b="hive" class="text">hello</Option> where I would just like it to be <Option b="hive">hello</Option>

因此它产生了 <选项b="hive" class="text"> hello,我希望它是 <选项b="hive"> hello

I am not sure how to do that. If I try to get a Nokogiri object by just feeding it the XML I want, it renders back exactly what I need with the internal text being within the <Option> tag set to children=[#<Nokogiri::XML::Text:0x80b9e3dc "hello">] and I don't know how to set that from builder.

我不知道该怎么做。如果我尝试通过输入我想要的XML来获得Nokogiri对象,它会返回我需要的,而内部文本在

If anyone has a reference to that in the Nokogiri documentation, I would appreciate it.

如果有人在Nokogiri文献中提到这一点,我将非常感激。

1 个解决方案

#1


31  

There are two approaches you can use.

有两种方法可以使用。

Using .text

使用。

You can call the .text method to set the text of a node:

您可以调用.text方法来设置节点的文本:

builder = Nokogiri::XML::Builder.new { |xml|
  xml.Transaction("requestName" => "OrderRequest") do
    xml.Option("b" => "hive"){ xml.text("hello") }
  end
}

which produces:

生产:

<?xml version="1.0"?>
<Transaction requestName="OrderRequest">
  <Option b="hive">hello</Option>
</Transaction>

Solution using text parameter

解决方案使用文本参数

Alternatively, you can pass the text in as a parameter. The text should be passed in before the attribute values. In other words, the tag is added in the form:

或者,您可以将文本作为参数传入。文本应该在属性值之前传入。换句话说,标签以以下形式添加:

tag "text", :attribute => 'value'

In this case, the desired builder would be:

在这种情况下,期望的构建器将是:

builder = Nokogiri::XML::Builder.new { |xml|
  xml.Transaction("requestName" => "OrderRequest") do
    xml.Option("hello", "b" => "hive")
  end
}

Produces the same XML:

产生相同的XML:

<?xml version="1.0"?>
<Transaction requestName="OrderRequest">
  <Option b="hive">hello</Option>
</Transaction>

#1


31  

There are two approaches you can use.

有两种方法可以使用。

Using .text

使用。

You can call the .text method to set the text of a node:

您可以调用.text方法来设置节点的文本:

builder = Nokogiri::XML::Builder.new { |xml|
  xml.Transaction("requestName" => "OrderRequest") do
    xml.Option("b" => "hive"){ xml.text("hello") }
  end
}

which produces:

生产:

<?xml version="1.0"?>
<Transaction requestName="OrderRequest">
  <Option b="hive">hello</Option>
</Transaction>

Solution using text parameter

解决方案使用文本参数

Alternatively, you can pass the text in as a parameter. The text should be passed in before the attribute values. In other words, the tag is added in the form:

或者,您可以将文本作为参数传入。文本应该在属性值之前传入。换句话说,标签以以下形式添加:

tag "text", :attribute => 'value'

In this case, the desired builder would be:

在这种情况下,期望的构建器将是:

builder = Nokogiri::XML::Builder.new { |xml|
  xml.Transaction("requestName" => "OrderRequest") do
    xml.Option("hello", "b" => "hive")
  end
}

Produces the same XML:

产生相同的XML:

<?xml version="1.0"?>
<Transaction requestName="OrderRequest">
  <Option b="hive">hello</Option>
</Transaction>