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).
| SQLState | Error Code | Error Text | Description |
|---|---|---|---|
| 22003 | SR354 | Too few arguments for XMLELEMENT | There must be at least one argument |
XMLELEMENT creates an 'Title' element without content.
select XMLELEMENT ('Title') from "Demo"."demo"."Employees";
callret
VARCHAR
_______________________________________________________________________________
<Title />
<Title />
. . .
9 Rows. -- 2 msec.
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>
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>