跳到主要内容

SetAttribute

为自定义 XML 节点设置属性。 如果属性已存在,则不会被修改。

语法

expression.SetAttribute(name, value);

expression - 表示 ApiCustomXmlNode 类的变量。

参数

名称必需/可选数据类型默认值描述
name必需string要设置的属性名称。
value必需string要分配给属性的值。

返回值

boolean

示例

此示例演示如何获取自定义 XML 节点的 XPath。

let doc = Api.GetDocument();
let xmlManager = doc.GetCustomXmlParts();
let xmlText = `
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">The Odyssey</title>
<author>Homer</author>
<year>-740</year>
<price>30.00</price>
</book>
</bookstore>`;
let xml = xmlManager.Add(xmlText);
let node = xml.GetNodes('/bookstore/book')[0];
node.SetAttribute('category', 'ancient');
let attributes = node.GetAttributes();
let paragraph = Api.CreateParagraph();
paragraph.AddText("Attributes of book node:\r\n");
attributes.forEach((attribute, index) => {
paragraph.AddText(`Attribute #${index} "${attribute.name}" with value: ${attribute.value}\r\n`);
});
doc.AddElement(0, paragraph);