# 22. Virtuoso Functions Guide - XMLELEMENT

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

## XMLELEMENT

Creates XML element
XMLELEMENT ( tag_name varchar, [list_of_attributes sequence], [child_or_attribute1 any], [child_or_attribute2 any], [...], [child_or_attributeN any]);
Description

XMLELEMENT takes an element name for identifier, an optional collection of attributes for the element, and arguments that make up the element's content. It returns a XML element. The second parameter may be omitted and at that time the rest parameters may be present. If one of the arguments is a call of the xpath_eval returning an attribute value, then this value would be added to element's content (not to element's attributes).

Parameters
tag_name – name of the element, it must be valid XML element name
list_of_attributes – a vector returned by XMLATTRIBUTES function. If the vector is NULL, then no attribute is created.
child_or_attributeI – a string, or name of a column, or concatenation of the names and/or strings, or a vector returned by XMLELEMENT, XMLFOREST, XMLCONCAT, or XMLAGG functions, or an entity object returned by corresponding functions (e.g. xtree_doc or xquery_eval). If the entity object is an attribute entity, then it is joined to the list of the element's attributes. If a parameter is NULL, then no child element or attribute is created for that parameter.
Errors

SQLState | Error Code | Error Text | Description
--- | --- | --- | ---
22003 | SR354 | Too few arguments for XMLELEMENT | There must be at least one argument

Examples
XMLELEMENT() with a single argument

XMLELEMENT creates an 'Title' element without content.

                  select XMLELEMENT ('Title') from "Demo"."demo"."Employees";
    callret
    VARCHAR
    _______________________________________________________________________________

    <Title />
    <Title />
    . . .
    9 Rows. -- 2 msec.

XMLELEMENT() with content

The following example produces an 'Emp' element with three attributes (the 'region' attribute is calculated by xquery_eval) and five nested subelements

    select XMLELEMENT ('Emp',
                        XMLATTRIBUTES ( "EmployeeID" AS "EmpID", "Title"),
                        XMLELEMENT ('Name', "FirstName" || ' ' || "LastName" ),
                        xquery_eval('//@region', xtree_doc ('<a region="WA"></a>')),
                        XMLFOREST ("PostalCode", "City" as "city"),
                        XMLCONCAT (XMLELEMENT ('HomePhone', "HomePhone"),
                                   XMLELEMENT ('BirthDate', "BirthDate")))
      from "Demo"."demo"."Employees"
      where "EmployeeID"=1;

    callret
    VARCHAR
    _______________________________________________________________________________

    <Emp EmpID="1" Title="Sales Representative" region="WA">
      <Name>Nancy Davolio</Name>
      <city>Seattle</city>
      <PostalCode>98122</PostalCode>
      <HomePhone>(206) 555-9857</HomePhone>
      <BirthDate>1948-12-08</BirthDate>
    </Emp>

XMLELEMENT() with the aggregate function XMLAGG()

This example produces 'Emp' elements, with the list of the 'Name' of all employees.

    select XMLELEMENT ('Emp',
                        XMLAGG (XMLELEMENT('Name', "FirstName", ' ', "LastName")))
      from "Demo"."demo"."Employees";
    callret
    VARCHAR
    _______________________________________________________________________________

    <Emp>
       <Name>Nancy Davolio</Name>
       <Name>Andrew Fuller</Name>
       <Name>Janet Leverling</Name>
       <Name>Margaret Peacock</Name>
       <Name>Steven Buchanan</Name>
       <Name>Michael Suyama</Name>
       <Name>Robert King</Name>
       <Name>Laura Callahan</Name>
       <Name>Anne Dodsworth</Name>
    </Emp>

See Also

XMLAGG()

XMLATTRIBUTE()

XMLCONCAT()

XMLFOREST()