You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用pugixml添加无值属性?

如何使用pugixml添加无值属性?

嘿,我来帮你搞定这个问题!你想要生成像 <option value="book" selected>book</option> 里的 selected 这种不带值的属性,但现在直接用 option_node.append_attribute("selected"); 会生成带空值的属性(比如 selected=""),对吧?

其实很简单,pugixml提供了控制属性格式的方法,你只需要在添加属性后,给这个属性设置特定的格式即可:

// 先添加属性
pugi::xml_attribute selected_attr = option_node.append_attribute("selected");
// 设置属性为无值格式
selected_attr.set_formatting(pugi::attribute_format_no_value);

这样设置之后,当你输出生成的HTML/XML时,这个selected属性就会变成你想要的无值形式,而不是带空引号的版本啦。

备注:内容来源于stack exchange,提问作者teusbenschop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 16:10:32