Reputation: 1571
Using the next SQL code, running with Oracle 10:
SELECT xmlserialize
(
document xmlelement
(
"Response", xmlforest
(
'00' AS "ReturnCode"
), xmlagg
(
xmlelement
(
"Students", xmlelement
(
"Student", xmlforest
(
'Some 1' AS "Name",
'1' AS "Id"
)
), xmlelement
(
"Student", xmlforest
(
'Some 2' AS "Name",
'2' AS "Id"
)
)
)
)
) AS CLOB INDENT
) FROM dual
... I get this XML structure:
<Response>
<ReturnCode>00</ReturnCode>
<Students>
<Student>
<Name>Some 1</Name>
<Id>1</Id>
</Student>
<Student>
<Name>Some 2</Name>
<Id>2</Id>
</Student>
</Students>
</Response>
... but, I want to get this one (removing the 'root' element):
<ReturnCode>00</ReturnCode>
<Students>
<Student>
<Name>Some 1</Name>
<Id>1</Id>
</Student>
<Student>
<Name>Some 2</Name>
<Id>2</Id>
</Student>
</Students>
Several attemps like this didnt work. Is mandatory to have a root element?:
SELECT xmlserialize
(
document xmlforest
(
'00' AS "ReturnCode"
), xmlagg
(
xmlelement
(
"Students", xmlelement
(
"Student", xmlforest
(
'Some 1' AS "Name",
'1' AS "Id"
)
), xmlelement
(
"Student", xmlforest
(
'Some 2' AS "Name",
'2' AS "Id"
)
)
)
) AS CLOB INDENT
) FROM dual
Any help will be appreciated. (This is just a simplification of something more complex I need to do in some project).
Upvotes: 0
Views: 277
Reputation: 6667
Removing the root element means you will no longer have a valid XML structure, but rather a sequence of XML structures. You could construct this by using xmlserialize(content ...)
instead of xmlserialize(document ....)
.
SELECT xmlserialize(content xmlforest(
'00' as "ReturnCode",
xmlforest(
xmlforest('Some 1' AS "Name",
'1' AS "Id"
) AS "Student",
xmlforest('Some 2' AS "Name",
'2' AS "Id"
) AS "Student"
) AS "Students"
) AS CLOB INDENT)
FROM DUAL;
You could also use xmlconcat
and xmlelement
instead of xmlforest
, if you prefer:
SELECT xmlserialize(content xmlconcat(
xmlelement("ReturnCode", '00'),
xmlelement("Students",
xmlelement("Student",
xmlelement("Name", 'Some 1'),
xmlelement("Id", '1')
),
xmlelement("Student",
xmlelement("Name", 'Some 2'),
xmlelement("Id", '2')
)
)
) AS CLOB INDENT)
FROM DUAL;
Upvotes: -1
Reputation: 9886
The easiest working way i could think is to use replace
. See below:
Select replace(Replace(col,'< >',''),'</ >','')
from
(
SELECT xmlserialize
(
document xmlelement
(
" ", xmlforest --Putting a wide space to differentiate from other tags
(
'00' AS "ReturnCode"
), xmlagg
(
xmlelement
(
"Students", xmlelement
(
"Student", xmlforest
(
'Some 1' AS "Name",
'1' AS "Id"
)
), xmlelement
(
"Student", xmlforest
(
'Some 2' AS "Name",
'2' AS "Id"
)
)
)
)
)
) AS COL
FROM dual )
Output:
<ReturnCode>00</ReturnCode>
<Students>
<Student>
<Name>Some 1</Name>
<Id>1</Id>
</Student>
<Student>
<Name>Some 2</Name>
<Id>2</Id>
</Student>
</Students>
Upvotes: 0
Reputation: 6346
The question is why you need this? XML should be "Well Formed"
This means:
- XML documents must have a root element
- XML elements must have a closing tag
- XML tags are case sensitive
- XML elements must be properly nested
- XML attribute values must be quoted
Add extract('/*/*')
and change document
-> content
SELECT xmlserialize
(
content xmlelement
(
"Response", xmlforest
(
'00' AS "ReturnCode"
), xmlagg
(
xmlelement
(
"Students", xmlelement
(
"Student", xmlforest
(
'Some 1' AS "Name",
'1' AS "Id"
)
), xmlelement
(
"Student", xmlforest
(
'Some 2' AS "Name",
'2' AS "Id"
)
)
)
)
).extract('/*/*') AS CLOB INDENT
) FROM dual
Upvotes: 3