我使用以下查询生成数据的XML表示。我希望输出像常规XML格式一样被认可。而是我收到一个连续的字符串。我试过在我的select语句中附加CHr(13)|| Chr(10),但是我得不到正确的输出。有人可以提供有关如何格式化XML的见解吗?谢谢!
SQL查询:
SELECT
XMLElement("Sample-Test" ,
XMLAgg(
XMLElement("Sample",
XMLElement("SAMPLE_NUM", s.sample_number),
XMLElement("LABEL_ID", s.label_id),
XMLElement("STATUS", s.status),
(SELECT
XMLAgg(
XMLElement("Test-Details",
XMLElement("TEST_NUM", t.test_number),
XMLElement("ANALYSIS", t.analysis),
(SELECT
XMLAgg(
XMLElement("Result-Details",
XMLElement("RESULT_NUM", R.RESULT_NUMBER),
XMLElement("RESULT_NAME", R.NAME)))
FROM RESULT R WHERE t.test_number = R.test_number
and t.SAMPLE_number = R.SAMPLE_NUMBER
)))
FROM TEST T WHERE t.SAMPLE_number = S.SAMPLE_NUMBER)))
).getClobVal() as XML
FROM sample s
WHERE s.sample_number = 720000020018;
电流输出: (CLOB)720000020018720000020018C21ALT_S90_NONSPE_00121Alanine Aminotransferase22GLUCOSE_S90_NONSPE_00122Glucose
预期产出:
<Sample-Test>
<Sample>
<SAMPLE_NUM>720000020018</SAMPLE_NUM>
<LABEL_ID>720000020018</LABEL_ID>
<STATUS>C</STATUS>
<Test-Details>
<TEST_NUM>21</TEST_NUM>
<ANALYSIS>ALT_S90_NONSPE_001</ANALYSIS>
<Result-Details>
<RESULT_NUM>21</RESULT_NUM>
<RESULT_NAME>Alanine Amino</RESULT_NAME>
</Result-Details>
</Test-Details>
</Sample>
</Sample-Test>
答案 0 :(得分:3)
您必须使用XMLSERIALIZE
:
SELECT
XMLSERIALIZE(DOCUMENT
XMLElement("Sample-Test" ,
XMLAgg(
XMLElement("Sample",
XMLElement("SAMPLE_NUM", s.sample_number),
XMLElement("LABEL_ID", s.label_id),
XMLElement("STATUS", s.status),
(SELECT
XMLAgg(
XMLElement("Test-Details",
XMLElement("TEST_NUM", t.test_number),
XMLElement("ANALYSIS", t.analysis),
(SELECT
XMLAgg(
XMLElement("Result-Details",
XMLElement("RESULT_NUM", R.RESULT_NUMBER),
XMLElement("RESULT_NAME", R.NAME)))
FROM RESULT R WHERE t.test_number = R.test_number
and t.SAMPLE_number = R.SAMPLE_NUMBER
)))
FROM TEST T WHERE t.SAMPLE_number = S.SAMPLE_NUMBER)))
) AS CLOB INDENT SIZE = 2) as XML
FROM sample s
WHERE s.sample_number = 720000020018;
修改强>
它不适合您,因为很可能您使用的是Oracle 10g,而版本11g中引入了INDENT
选项。如果是这种情况,请使用EXTRACT('*')
:
SELECT
XMLElement("Sample-Test" ,
XMLAgg(
XMLElement("Sample",
XMLElement("SAMPLE_NUM", s.sample_number),
XMLElement("LABEL_ID", s.label_id),
XMLElement("STATUS", s.status),
(SELECT
XMLAgg(
XMLElement("Test-Details",
XMLElement("TEST_NUM", t.test_number),
XMLElement("ANALYSIS", t.analysis),
(SELECT
XMLAgg(
XMLElement("Result-Details",
XMLElement("RESULT_NUM", R.RESULT_NUMBER),
XMLElement("RESULT_NAME", R.NAME)))
FROM RESULT R WHERE t.test_number = R.test_number
and t.SAMPLE_number = R.SAMPLE_NUMBER
)))
FROM TEST T WHERE t.SAMPLE_number = S.SAMPLE_NUMBER)))
).EXTRACT('*') as XML
FROM sample s
WHERE s.sample_number = 720000020018;