Found a question by asking the same question myself. The solution I came up with is to use a common table expression with DISTINCT.
WITH q1 (id, specialization) AS
(
SELECT DISTINCT id, specialization
FROM table_name
)
SELECT q1.id,
XMLELEMENT(
NAME "Specializations",
XMLAGG(
XMLELEMENT(NAME "Specialization", q1.specialization)))
FROM q1
GROUP BY q1.id
In your case, it would be simpler and more understandable to use subqueries instead (for clarity, a correction of the XMLELEMENT template is output):
SELECT t.id, XMLAGG(q1.specialization), XMLAGG(q2.basic_skill2),
XMLAGG(q3.basic_skill1)
FROM table_name t,
(SELECT DISTINCT id, specialization FROM table_name) q1,
(SELECT DISTINCT id, basic_skill2 FROM table_name) q2,
(SELECT DISTINCT id, basic_skill1 FROM table_name) q3
WHERE t.id = q1.id AND t.id = q2.id AND t.id = q3.id
GROUP BY t.id
It may be easier, but this is what I came up with.
In addition, you can use features such as XMLQUERY and XSLTRANSFORM. Much easier and fewer errors than the manual way you do it.