以下のクエリがあります。
SELECT DISTINCT P.PLAYERID, P.FIRSTNAME, P.LASTNAME, P.POSITION,PT.POINTS, PT.MINUTES, CALC_TENDEX(P.PLAYERID, PT.TEAMID, 2009) TENDEX, T.DIVISION, PT.TEAMID, T.TEAMNAME
FROM PLAYERS P
JOIN PLAYERS_TEAMS PT ON P.PLAYERID = PT.PLAYERID
JOIN TEAMS T ON PT.TEAMID=T.TEAMID
WHERE PT.YEAR=2009 AND T.DIVISION='East'
ORDER BY TENDEX DESC
FETCH FIRST 12 ROWS ONLY;
このクエリを次のようなカスタム形式でxmlとして出力したいと思います。
<nba dataset="topplayers">
<division ID="East">
<player></player>
.
.
.
<player></player>
</nba>
SQL/XMLを使用してみましたが、何をしようとしても必要な回答が返されません。それを直接xmlに変換する簡単な方法はありますか?
編集:私が試したSQL/XML(これのさまざまな変更とともに)
SELECT DISTINCT XMLELEMENT("nba",
XMLAGG (
XMLELEMENT("division",
XMLATTRIBUTES(T.DIVISION AS "ID"),
(SELECT DISTINCT XMLAGG(
XMLELEMENT("player",
XMLFOREST(
P.PLAYERID AS "playerid",
P.FIRSTNAME AS "firstname",
P.LASTNAME AS "lastname",
P.POSITION AS "position",
PT.POINTS AS "points",
PT.MINUTES AS "minutes",
CALC_TENDEX(PT.PLAYERID, PT.TEAMID, 2009) AS "tindex",
T.DIVISION AS "division",
PT.TEAMID AS "teamid",
T.TEAMNAME AS "teamname"
)
)
)
FROM PLAYERS P
JOIN PLAYERS_TEAMS PT ON P.PLAYERID = PT.PLAYERID
JOIN TEAMS T ON PT.TEAMID=T.TEAMID
WHERE PT.YEAR=2009 AND T.DIVISION='East' AND ROWNUM < 12
)
)
)
) AS "nba"
FROM TEAMS T
WHERE T.DIVISION = 'East';
SQLをXMLに直接変換する簡単な方法。次に例を示します。
SQL> select dbms_xmlgen.getxml('select * from hr.employees where employee_id = 100')
as xml from dual;
XML
--------------------------------------------------------------------------------
<?xml version="1.0"?>
<ROWSET>
<ROW>
<EMPLOYEE_ID>100</EMPLOYEE_ID>
<FIRST_NAME>Steven</FIRST_NAME>
<LAST_NAME>King</LAST_NAME>
<EMAIL>SKING</EMAIL>
<PHONE_NUMBER>515.123.4567</PHONE_NUMBER>
<HIRE_DATE>17-JUN-03</HIRE_DATE>
<JOB_ID>AD_PRES</JOB_ID>
<SALARY>24000</SALARY>
<DEPARTMENT_ID>90</DEPARTMENT_ID>
</ROW>
</ROWSET>
XMLの固定フォーマットがある場合、それをXSLTで記述し、 [〜#〜] xmltransform [〜#〜] を使用して残りの部分を処理できます。