Publicar datos SQL como XML
Puede utilizar las funciones XMLElement(), XMLConcat(), XMLAgg() y XMLAttributes() dentro de una expresión SQL para transformar los resultados de una consulta de bases de datos en XML.
select XMLElement('Parent', 'Parent Text');
<Parent>Parent Text</Parent>
XMLELEMENT
-----------
XML
(1 row)
select XMLSerialize(XMLElement('Parent', 'Parent Text'));
XMLSERIALIZE
------------------------------
<Parent>Parent Text</Parent>
(1 row)
select
XMLElement('Parent',
XMLElement('Child', 'Child text'));
<Parent>
<Child>Child text</Child>
</Parent>
select
XMLElement('Parent',
XMLElement('Child',
XMLElement('GrandChild', 'Grandchild text')));
<Parent>
<Child>
<GrandChild>Grandchild text</GrandChild>
</Child>
</Parent>
DEPTNO DEPTNAME DEPTLOC
------ ---------- ---------
10 MARKETING BOSTON
20 HR BOSTON
30 SALES NEW YORK
40 ENGINEERING NEW YORK
select * from departments;
<Departments>
<Dept>
<Number>10</Number>
<Name>MARKETING</Name>
<Location>BOSTON</Location>
</Dept>
<Dept>
<Number>20</Number>
<Name>HR</Name>
<Location>BOSTON</Location>
</Dept>
<Dept>
<Number>30</Number>
<Name>SALES</Name>
<Location>NEW YORK</Location>
</Dept>
<Dept>
<Number>40</Number>
<Name>ENGINEERING</Name>
<Location>NEW YORK</Location>
</Dept>
</Departments>
select
XMLElement('Departments', XMLAGG(
XMLElement('Dept', XMLConcat(
XMLElement('Number', d.deptno),
XMLElement('Name', d.deptname),
XMLElement('Location', d.deptloc)))))
from departments d;
En cada una de las dos primeras llamadas XMLElement(), el contenido del elemento se crea mediante una llamada a la función XML anidada. Para crear un documento XML estructurado de forma jerárquica de nodos principales y secundarios, anide las llamadas XMLElement() dentro de una sentencia SQL.
XMLElement('Departments', XMLAgg (
El agregado XMLAgg() se utiliza para el segundo argumento, indicando que el contenido para el nodo <Departamentos> de nivel superior es un grupo de nodos agregados, lo que significa que estos nodos serán nodos hijos de un único nodo padre.
XMLElement('Dept', XMLConcat(
XMLElement('Number', d.deptno),
XMLElement('Name', d.deptname),
XMLElement('Location', d.deptloc)))))
Estas tres llamadas ' XMLElement() ' incrustadas crean tantos nodos hijos <Dept> como sean necesarios para envolver las filas de datos que se devuelven de la tabla DEPARTMENTS.
<Departments>
<Dept>
<Number>10</Number>
<Name>MARKETING</Name>
<Location>BOSTON</Location>
</Dept>
</Departments>
<Departments>
<Dept>
<Number>20</Number>
<Name>HR</Name>
<Location>BOSTON</Location>
</Dept>
</Departments>
<Departments>
<Dept>
<Number>30</Number>
<Name>SALES</Name>
<Location>NEW YORK</Location>
</Dept>
</Departments>
<Departments>
<Dept>
<Number>40</Number>
<Name>ENGINEERING</Name>
<Location>NEW YORK</Location>
</Dept>
</Departments>
Esto no es una sintaxis XML válida porque hay cuatro instancias del elemento del documento <Departments>. Esto demuestra lo importante que es utilizar la función IsValidXML() para asegurarse de que el XML creado con la biblioteca de funciones se puede analizar como XML. Además, si va a utilizar esquemas, también será responsable de elaborar XML con formato correcto (XML que se ajuste a la estructura especificada por el esquema).
<EmployeesByDepartment>
<Dept DeptNo=“10“>
<Name>ACCOUNTING</Name>
<Location>NEW YORK</Location>
<Employees>
<Employee EmpNo=“7782“>
<Name>CLARK</Name>
<Job>MANAGER</Job>
<Manager>7839</Manager>
<Salary>2450</Salary>
</Employee>
<Employee EmpNo=“7839“>
<Name>KING</Name>
<Job>PRESIDENT</Job>
<Salary>5000</Salary>
</Employee>
...
</Employees>
</Dept>
...
<EmployeesByDepartment>
CREATE temp table emp_grouping AS
SELECT deptno, XMLElement ('Employees', XMLAGG (
XMLElement ('Employee', XMLAttributes ('EmpNo', empno),
XMLConcat (
xmlelement ('name', name),
xmlelement ('job', job),
xmlelement ('manager', mgr),
xmlelement ('salary', sal),
xmlelement ('comm', comm)))))
AS xml FROM emp INNER JOIN dept
ON emp.deptno = dept.deptno
GROUP BY deptno;
SELECT XMLElement('EmployeesByDepartment', XMLAGG(
XMLElement('Dept', XMLAttributes('DeptNo', deptno), XMLConcat(
XMLElement('Name', D.DNAME),
XMLElement('Location', D.LOC),
emp_grouping.xml))))
FROM dept INNER JOIN emp_grouping
ON dept.deptno = emp_grouping.deptno;