# 22. Virtuoso Functions Guide - XMLAddAttribute

Chapter Contents | Prev | Next
Keyword Search:
www.openlinksw.com
docs.openlinksw.com
Book Home
Contents
Preface
Virtuoso Functions Guide
Administration
Aggregate Functions
Array Manipulation
BPEL APIs
Backup
Compression
Cursor
Date & Time Manipulation
Debug
Dictionary Manipulation
Encoding & Decoding
File Manipulation
Free Text
Hashing / Cryptographic
LDAP
Locale
Mail
Miscellaneous
Number
Remote SQL Data Source
Replication
SOAP
SQL
String
Transaction
Type Mapping
UDDI
User Defined Types & The CLR
Virtuoso Java PL API
Virtuoso Server Extension Interface (VSEI)
Web Server & Internet
XML
xmlagg
xmlattributes
xmladdattribute
xmlappendchildren
xmlconcat
xmlelement
xmlforest
xmlinsertafter
xmlinsertbefore
xmlreplace
xmltype.xmltype
xmltype.createnonsch...
xmltype.createschema...
xmltype.createxml
xmltype.existsnode
xmltype.extract
xmltype.getclobval
xmltype.getnamespace
xmltype.getnumval
xmltype.getrooteleme...
xmltype.getschemaurl
xmltype.getstringval
xmltype.isfragment
xmltype.isschemabase...
xmltype.isschemavali...
xmltype.isschemavali...
xmltype.schemavalida...
xmltype.setschemaval...
xmltype.toobject
xmltype.transform
xmlupdate
xper navigation
createxml
isentity
serialize_to_utf8_xm...
tidy_html
tidy_list_errors
updatexml
xml_add_system_path
xml_auto
xml_auto_dtd
xml_auto_schema
xml_create_tables_fr...
xml_cut
xml_doc_output_optio...
xml_get_system_paths
xml_load_mapping_sch...
xml_load_schema_decl
xml_namespace_scope
xml_persistent
xml_template
xml_tree
xml_tree_doc
xml_tree_doc_media_t...
xml_uri_get
xml_validate_dtd
xml_validate_schema
xml_view_dtd
xml_view_schema
xmlsql_update
xpath_eval
xper_cut
xper_doc
xper_locate_words
xpf_extension
xpf_extension_remove
xquery_eval
xslt
xslt_format_number
xslt_sheet
xslt_stale
xte_head
xte_node
xte_node_from_nodebl...
xte_nodebld_acc
xte_nodebld_final
xte_nodebld_init
xtree_doc
XPATH & XQUERY
Functions Index

## XMLAddAttribute

Add an attribute to the given node of an XML tree document
integer XMLAddAttribute (inout source any, in mode integer, in attr_name any, [in attr_value any]);
Description

The function modifies the source document by adding an attribute to the current node of source entity. The source should be an XML tree entity, not "persistent XML" one, should be an element entity (not attribute, text etc.) and should not be a root entity. Parameters attr_name and attr_value can be of any types that can be casted to string. attr_name can be an attribute entity, in this case both the name and the value of attr_name attribute is used and attr_value parameter must be omited.

The mode specifies how to resolve duplicate attribute names, if an source entity already has an attribute whose name is equal to attr_name. Mode 0 is similar to "insert into": the function signals an error if an attribute already exists. Mode 1 is similar to "insert soft": the function do nothing if an attribute already exists and not signalling an error. Mode 2 is similar to "insert replacing": the function either adds a new attribute or replacing the value of existing attribute.

Parameters
source – The XML tree entity to change.
mode – The mode of resolving duplicate attribute names.
attr_name – The name of a new attribute (or an attribute XML entity that specifies both name and value of an attribute.
attr_value – The value of a new attribute
Return Types

The function returns integer that indicate the actual operation performed. The zero value means that there was no change ("insert soft" when an attribute already exists). The value one means that a new attribute was added, the value two means that an existing attribute was replaced.

Examples
Different modes of calling XMLAddAttribute

The sample procedure contains three calls of XMLAddAttribute with different values of mode, for "insert into", "insert soft" and "insert replacing" behaviour.

    create procedure XMLAddAttribute_demo ()
    {
      declare DESCRIPTION varchar (33);
      declare XMLENTITY, ent any;
      result_names (DESCRIPTION, XMLENTITY);
      result ('EXAMPLE 1', 'Adding a new attribute');
      ent := xpath_eval ('//b', xtree_doc('<a><b attrX="x" attrY="y"/></a>'));
      result ('The document to modify:', xpath_eval('/', ent));
      result ('An element node to change:', ent);
      XMLAddAttribute (ent, 0, 'attrZ', 'z');
      result ('The document after modification:', xpath_eval('/', ent));
      result ('EXAMPLE 2', 'Failed adding of an (existing) attribute');
      ent := xpath_eval ('//b', xtree_doc('<a><b attrX="x" attrY="y"/></a>'));
      result ('The document to modify:', xpath_eval('/', ent));
      result ('An element node to change:', ent);
      XMLAddAttribute (ent, 1, 'attrX', 'xNEW');
      result ('The document after modification:', xpath_eval('/', ent));
      result ('EXAMPLE 3', 'Successful replacing of an existing attribute');
      ent := xpath_eval ('//b', xtree_doc('<a><b attrX="x" attrY="y"/></a>'));
      result ('The document to modify:', xpath_eval('/', ent));
      result ('An element node to change:', ent);
      XMLAddAttribute (ent, 2, 'attrX', 'xNEW');
      result ('The document after modification:', xpath_eval('/', ent));
    }

    Done. -- 00000 msec.

    XMLAddAttribute_demo()
    DESCRIPTION                                  XMLENTITY
    VARCHAR                                      VARCHAR
    _______________________________________________________________________________

    EXAMPLE 1                          Adding a new attribute
    The document to modify:            <a><b attrX="x" attrY="y" /></a>
    An element node to change:         <b attrX="x" attrY="y" />
    The document after modification:   <a><b attrX="x" attrY="y" attrZ="z" /></a>
    EXAMPLE 2                          Failed adding of an (existing) attribute
    The document to modify:            <a><b attrX="x" attrY="y" /></a>
    An element node to change:         <b attrX="x" attrY="y" />
    The document after modification:   <a><b attrX="x" attrY="y" /></a>
    EXAMPLE 3                          Successful replacing of an existing attribute
    The document to modify:            <a><b attrX="x" attrY="y" /></a>
    An element node to change:         <b attrX="x" attrY="y" />
    The document after modification:   <a><b attrX="xNEW" attrY="y" /></a>

    12 Rows. -- 00000 msec.

See Also

XMLReplace

XMLAppendChildren

XMLInsertBefore

XMLInsertAfter