SQL データを XML として公開する操作
SQL 式の中で XMLElement() 関数、XMLConcat() 関数、XMLAgg() 関数、および XMLAttributes() 関数を使用して、データベース照会の結果を 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;
最初の 2 つの XMLElement() 呼び出しのそれぞれにおいて、要素の内容がネストした XML 関数の呼び出しによって作成されます。 親ノードと子ノードの階層的な構造化 XML 文書を作成するには、XMLElement() 呼び出しを SQL ステートメントの中でネストします。
XMLElement('Departments', XMLAgg (
2番目の引数には'XMLAgg()アグリゲートが使用され、トップレベルの<Departments>ノードのコンテンツがアグリゲートされたノード・グループであることを示しています。
XMLElement('Dept', XMLConcat(
XMLElement('Number', d.deptno),
XMLElement('Name', d.deptname),
XMLElement('Location', d.deptloc)))))
これら3つの埋め込み'XMLElement()呼び出しは、DEPARTMENTSテーブルから返されるデータ行を包むのに必要な数の<Dept>子ノードを作成する。
<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>
これは、<Departments> 文書要素のインスタンスが 4 つあるため、有効な XML 構文ではありません。 この例により、関数ライブラリーで作成する XML が XML として解析できることを IsValidXML() 関数を使用して確認することがいかに重要であるかが分かります。 また、スキーマを使用している場合には、整形式の XML (スキーマで指定された構造に従った XML) を返す必要もあります。
<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;