Home > Language Reference > XML Functions > SetXMLAttribute

SetXMLAttribute

The SetXMLAttribute function modifies or adds the specified attribute in an XML file.

Syntax

ret = SetXMLAttribute(<XML_filename>,<XML_path>,<attribute_name>,<attribute_value>)

Parameters

<XML_filename>. string, name of the XML file.

<XML_path>. string, XML path (case insensitive). See Remarks below to know how is built such a path.

<attribute_name>, string, attribute name. It can be too "<text>" (surrounded by the ") to add the <attribute_value> text.

<attribute_value>, string, new value for <attribute_name>.

Return value

ret, optional integer return value. If the function succeeds, the return value is zero. If the function fails, the return value is a nonzero value (see XML Functions Error Codes).

Remarks

A XML path is a list of node descriptors separated by the \ character.

The XML path can include tagnames such as:
tagname[attributename1='value']
The tagnames are strings without double quotes, they cannot contain spaces or reserved characters. 

The XML path can include attribute names. They are strings without double quotes, OR it can be the reserved keyword <text> or the reserved keyword <index>. <text> is used to specify the node inner text, <index> is used to specify the node index (first one starts at 1) when more nodes with the same tag and attributes exist within the same parent. Several attributes can be listed with the syntax tagname[attributename1='value1', attributename2='value2'], for example :
If the XML file contains:
< bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
the <XML_path> can be "bookstore\book[genre='autobiography']" or "bookstore\book[publicationdate='1981']" or "bookstore\book[publicationdate='1981',genre='autobiography']"

See also

AppendXMLNode
EnumXMLAttributes
EnumXMLChildren
GetXMLAttribute

Example

XML file:
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
</bookstore>

'This script replaces in ISBN the value "1-861003-11-0" by "3-8610"
file$ = "c:\wttest\sample.xml"
setxmlattribute(file$, "bookstore\book[publicationdate='1981']", "ISBN", "3-8610")
'Those 2 other syntaxes give the same result:
setxmlattribute(file$, "bookstore\book[genre='autobiography']", "ISBN", "3-8610")
setxmlattribute(file$, "bookstore\book[publicationdate='1981',genre='autobiography']", "ISBN", "3-8610")

XML file:
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price></price>
</book>
</bookstore>

'Add a text in the <price></price> node. To add 8.99 price, the syntax is:
file$ = "c:\wttest\sample.xml"
setxmlattribute(file$, "bookstore\book[publicationdate='1981']\price","<text>","8.99")

XML file:
<bookstore>
<book genre="autobiography" publicationdate="1981">
<title>The Autobiography of Franklin</title>
<author>
<name></name>
<name></name>
</author>
<price>8.99</price>
</book>
<book genre="autobiography" publicationdate="1981">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name></first-name>
<last-name></last-name>
</author>
<price>9.01</price>
</book>
</bookstore>

'To add in the first <name> node Benjamin and in the second <name> Todd - as there is
' no way to identify <name> first or second block, <index> has to be used:
file$ = "c:\wttest\sample.xml"
setxmlattribute(file$, "bookstore\book\author\name[<index>=1]","<text>","Benjamin")
setxmlattribute(file$, "bookstore\book\author\name[<index>=2]","<text>","Todd")