SetXMLAttribute
File management function.
The SetXMLAttribute function modifies or adds an attribute in the specified XML file. Not available in WinTask Lite
Syntax
SetXMLAttribute(<filename>,<XML_path>,<attribute_name>,<value>)
or
ret=SetXMLAttribute(<filename>,<XML_path>,<attribute_name>,<value>)
Parameters
<filename>, string, name of the XML file where to add/modify the attribute.
<XML_path>, string, list of node descriptors separated by the \ character to go where the attribute is. The string is not case-sensitive.The structure of such a path is:
tagname[attributename1='value']
The tagnames are strings without double quotes, they cannot contain spaces or reserved characters.
The attribute names 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']
Examples:
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']"<attribute_name>, string, name of the attribute - not surrounded by double quotes, OR keyword <text> surrounded by double quotes to add the text as specified in <value>.
<value>, string, value for the specified attribute
Return value
Ret, numeric return code. When the attribute value is successfully inserted in the XML file, the function returns 0, otherwise use this return code for Error Handling. The error codes are :
20
The XML file could not be saved
27
The XML file could not be opened
90
Internal error (COM error when invoking XML COM)
99
Invalid parameters
110
Invalid XML path
See also
Examples
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 code will replace in ISBN the value "1-861003-11-0" by "3-8610"
file$ = "c:\program files\wintask\scripts\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>We need to add a text in the <price></price> node. To add 8.99 price, the syntax is:
file$ = "c:\program files\wintask\scripts\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>We need 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:\program files\wintask\scripts\sample.xml"
setxmlattribute(file$, "bookstore\book\author\name[<index>=1]","<text>","Benjamin")setxmlattribute(file$, "bookstore\book\author\name[<index>=2]","<text>","Todd")