如何向Nokogiri节点添加属性? [英] How to add attribute to Nokogiri node?

查看:44
本文介绍了如何向Nokogiri节点添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向现有的 Nokogiri 节点添加一个属性.我所做的是:

I'm trying to add an attribute to an existing Nokogiri node. What I've done is this:

node.attributes['foobar'] = Nokogiri::XML::Attr.new('foo', 'bar')

但我收到错误:

TypeError Exception: wrong argument type String (expected Data)

什么是Data 数据类型,以及如何向 Nokogiri 对象添加属性?

What is a Data data type, and how do I add an attribute to the Nokogiri object?

谢谢!

推荐答案

我相信你应该只需要使用 []= 方法,即

I believe you should just need to use the []= method, i.e.

node['foo'] = 'bar'

你也可以使用 node.set_attribute('foo', 'bar').

这篇关于如何向Nokogiri节点添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆