Building xml in oracle without unnecessary tags



I wanted to build XML in oracle using sql, I found a way building it using sql functions after a little research, I am completely unaware of any XML. My query is



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';


My problem is for using xmlforest, I had to use alias name 'sap' in my query to work and to get xml output. If i am using that, I am getting an unwanted tag 'SAP' around my data which I have to later cut it, which is unacceptable. Is there a way I can get rid of this extra 'SAP' tag without having to cut. The output data is



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


Any help is really appreciated and thanks in advance.


No comments:

Post a Comment