3

我正在使用以下查询来生成数据的 XML 表示。我希望输出能够像常规 XML 格式一样被识别。相反,我收到一个连续的字符串。我尝试在我的选择语句中附加 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_00121丙氨酸氨基转移酶22GLUCOSE_S90_NONSPE_00122葡萄糖

预期输出:

<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>
4

1 回答 1

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,并且该INDENT选项是在 11g 版本中引入的。如果是这种情况,请尝试以下方法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;
于 2013-11-12T21:37:43.213 回答