# 22. Virtuoso Functions Guide - XMLUpdate

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

## XMLUpdate

Create a changed copy of given document by replacing some nodes.
any XMLUpdate (inout source any, in path1 varchar, in replacement1 any, in path2 varchar, in replacement2 any, ... , in pathN varchar, in replacementN any);
Description

The function takes of the XML document referenced by source XML tree entity, makes a copy of that document; then it modifies the copy by finding fragments that are values of path1, path2, ..., pathN XPATH expressions and replacing them with values specified by replacement1, replacement2, ...,replacementN; the modified copy is returned as the result of the function call.

Every pathI should be a string that is a correct XPATH expression. Every such expression is evaluated according to the rules for XPATH expressions in XSLT (attribute entities are not cast to their string values). The context node is source, context size and position are both equal to 1.

Every replacementI may be an XML tree entity, a NULL or a value of some other type that will be converted to varchar before use.

For simplicity, consider the case when there is only one path1 XPATH expression parameter and only one replacement1. First of all, a copy of the document of the source XML tree entity is created. Then path1 is evaluated.

If the result of evaluation is not an XML entity then no replacement is made and the copy of the source document is returned unchanged. If the result is an XML entity that is not in the source document (e.g. an entity returned by the call of document(...) function.) then no replacement is made either.

If the result of evaluation is an XML entity in the source document then the function finds a node in the copied document that is a copy of the node in the source that is returned by XPATH evaluation. This node in the copied document will be referred to as a "highlighted" node.

If the result of evaluation is an attribute entity then the value of the attribute in the highlighted node is changed. If replacement1 is NULL then the attribute is removed at all, otherwise its value is set to the string value of the replacement1.

If the result of evaluation is not an attribute entity but a entity then the highllighted node is replaced with the value of replacement1. The method of replacement depends on the type of the replacement value. If replacement1 is NULL then the highlighted node is simply removed from the result; if the node is the root or a single child of the root then function immediately returns NULL instead of an XML entity (because XML document can not be totally empty), otherwise the parent of the highlighted node will simply have one child less. If replacement1 is an non-empty string then the highlighted node is replaced with a new text node whose string value is equal to replacement1. If replacement1 is an empty string then the effect is exactly the same as in case of NULL because the string value of a text node can not be empty. If replacement1 is an XML entity that is a root entity of some document then the highlighted node is replaced with a sequence of copies of all children of that root. If replacement1 is an XML entity of some other sort (XML element, text, comment etc.) then the that is not a root entity of some document then the highlighted node is replaced with a sequence of copies of all children of that root.

After editing the copied document, some normalization may happen. The modified document may contain adjacent text nodes; every sequence of such nodes is replaced with one text node whose string value is a concatenation of string values of that nodes. E.g. if the highlighted node is an element <a/> that resides between two text nodes "text1" and "text2" and the highlighted node is removed, then these text nodes become neighbors; normalization will replace them with a single text node "text1text2"

Now consider the case of more than one replacement made in one function call. If more than one pair of XPATH expression and replacement is given then the function first finds all highlighted nodes, all together, then it performs all replacements, then it performs an normalization. It is very important to understand that the order of making replacements has nothin to do with the order of pairs of parameters in the function call. If some highlighted node is an ancestor of some other highlighted node then the effect of the replacement of the descendant highlighted node is void: the descendant is replaced first; then the ancestor is replaced as a whole, no matter if some of its descendants are modified. If a node is the result of two different XPATH expressions ("highlighted more than once"), the order of parameters in the function call is used to resolve ambiguity: the last pair of parameters will have a higher priority.

Parameters
source – A source XML tree entity. It is an error to pass persistent XML entity as an argument.
pathI – A text of XPATH expression to be used in order to find nodes to replace.
replacementI – A replacement value for XML nodes found by pathI; it may be NULL or XML tree entity or a string r anything else but not an persistent XML entity.
Return Types

If the source parameter is an XML tree entity then the function returns an XML tree entity that points to the root of the modified copy of the source document or a NULL. If the source is an instance of XMLType then a non-schema based instance of XMLType is created from the resulting XML tree entity.

Examples
A simple replacement of one element with other

The call of function XMLUpdate replaces element 'c' with a copy of element 'replacement'.

    select XMLUpdate (xtree_doc('<a><b><q/>-<c>Hello</c>-</b></a>'), '//c', xtree_doc('<replacement/>'))
    callret
    VARCHAR
    _______________________________________________________________________________

    <a><b><q />-<replacement />-</b></a>

Two replacements in parallel

Elements titled 'c1' and 'c3' are replaced with text nodes.

    select XMLUpdate (xtree_doc('<a><b><c1>Hello1</c1><c2>Hello2</c2><c3>Hello3</c3></b></a>'), '//c1', 'world1', '//c3', 'world3')
    callret
    VARCHAR
    _______________________________________________________________________________

    <a><b>world1<c2>Hello2</c2>world3</b></a>

See Also

XMLReplace

XMLAddAttribute

XMLAppendChildren

XMLInsertBefore

XMLInsertAfter

xslt