在oracle中构建xml而没有不必要的标记

时间:2014-07-31 18:30:55

标签: sql xml oracle oracle11g

我想用oql在oracle中构建XML,经过一番研究后我发现了一种使用sql函数构建它的方法,我完全不知道任何XML。我的问题是

SELECT XMLELEMENT("orderwave",
             XMLAGG(XMLELEMENT("order_header",
                         -- XMLATTRIBUTES(t2.l_name AS "order_number"),
                          XMLFOREST(
                          nvl(t2.l_name,' ') as "order_number",
                        (
                         SELECT XMLAGG(XMLELEMENT("order_line",
                                       XMLFOREST( nvl(t1.l_name,' ') AS "label_type", 
                                                  nvl(t1.l_num,0) AS "lpn",
                                                  nvl(t1.l_num,0) AS "sku"
                                                )
                                                  )
                                      )  
                          FROM test_table t1
                          WHERE UPPER(t1.record_type)='D'             
                        ) AS sap
                                  )
                                  )
                       )
                       ) AS HEADER
                       FROM test_table t2 
                       WHERE UPPER(t2.record_type) ='H';  

我的问题是使用xmlforest,我不得不在我的查询中使用别名“sap”来工作并获得xml输出。如果我正在使用它,我会在我的数据周围收到一个不需要的标签“SAP”,我必须在以后剪切它,这是不可接受的。有没有办法我可以摆脱这个额外的'SAP'标签,而不必削减。输出数据是

<orderwave>
   <order_header>
      <order_number>order1</order_number>
      <SAP>
         <order_line>
            <label_type>test1</label_type>
            <lpn>1</lpn>
            <sku>1</sku>
         </order_line>
         <order_line>
            <label_type>test2</label_type>
            <lpn>2</lpn>
            <sku>2</sku>
         </order_line>
         <order_line>
            <label_type />
            <lpn>0</lpn>
            <sku>0</sku>
         </order_line>
      </SAP>
   </order_header>
   <order_header>
      <order_number>order2</order_number>
      <SAP>
         <order_line>
            <label_type>test1</label_type>
            <lpn>1</lpn>
            <sku>1</sku>
         </order_line>
         <order_line>
            <label_type>test2</label_type>
            <lpn>2</lpn>
            <sku>2</sku>
         </order_line>
         <order_line>
            <label_type />
            <lpn>0</lpn>
            <sku>0</sku>
         </order_line>
      </SAP>
   </order_header>
</orderwave>

任何帮助都非常感谢并提前感谢。

1 个答案:

答案 0 :(得分:1)

我找到了答案

select xmlelement  
       (  
          "order_wave"  
        , xmlagg  
          (  
            xmlelement  
            (  
                "order_header"  
              , xmlelement("order_number", nvl(t1.l_name,' '))  
              , xmlagg  
                (  
                  xmlelement  
                  (  
                     "order_line"  
                   , xmlelement("label_type", nvl(t2.l_name,' '))  
                   , xmlelement("lpn", nvl(t2.l_num,0))  
                   , xmlelement("sku", nvl(t2.l_num,0))  
                  )  
                )  
            )  
          )  
       ) xml_data  
  from test_table t1, test_table t2  
 where t1.record_type = 'h'  
   and t2.record_type = 'd'
 and t1.msg_ind = t2.msg_ind  
 group  
    by t1.l_name;