跳到主要内容

SetAttribute

Sets an attribute for the custom XML node. If the attribute already exists, it will not be modified.

Syntax

expression.SetAttribute(name, value);

expression - A variable that represents a ApiCustomXmlNode class.

Parameters

NameRequired/OptionalData typeDefaultDescription
nameRequiredstringThe name of the attribute to set.
valueRequiredstringThe value to assign to the attribute.

Returns

boolean

Example

This example shows how to get the XPath of a custom XML node.

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);