I have a function in oracle that return a XMLType
, but without root element because the root element can repeat, and I need that to use in another select with XMLElements
My function is:
CREATE OR REPLACE FUNCTION F_XML_WITHOUT_ROOT
RETURN XMLTYPE AS
vXML CLOB;
vXML_TEMP CLOB;
BEGIN
vXML := '';
FOR cI IN (
SELECT
'10' ALM_IN_CODIGO,
'1' LOC_IN_CODIGO,
'DE' NAT_ST_CODIGO
FROM DUAL
UNION ALL
SELECT
'10' ALM_IN_CODIGO,
'2' LOC_IN_CODIGO,
'DE' NAT_ST_CODIGO
FROM DUAL
) LOOP
SELECT
(
XMLSerialize(content
XMLElement("Item"
, XMLForest(
cI.ALM_IN_CODIGO AS ALM
, cI.LOC_IN_CODIGO AS LOC
, cI.NAT_ST_CODIGO AS NAT
) Item
).extract('/*') indent
)
) XML_BL_XML
INTO vXML_TEMP
FROM DUAL;
vXML := vXML || vXML_TEMP;
END LOOP;
RETURN XMLTYPE.CreateXML(vXML);
END F_XML_WITHOUT_ROOT;
When I called that:
SELECT
MGCLI.F_XML_WITHOUT_ROOT()
FROM DUAL;
I received the error code
ORA-31011: XML parsing failed
I think that's have a better way to make this work, but I don't know how
<Item><ALM>10</ALM><LOC>1</LOC><NAT>DE</NAT></Item>
<Item><ALM>10</ALM><LOC>2</LOC><NAT>DE</NAT></Item>
The right function is called in this select to make the entire XML
SELECT
XMLSerialize(content
XMLElement("Group", XMLAttributes('I' AS "OP")
, XMLForest(
3 FIL
, 59 TPD
)
, XMLElement("Obs"
, XMLForest(
'N' OB_CH_TYPE
, (
'Ref. '
) OB_ST_OBS
)
)
, (
SELECT
XMLAgg(
XMLElement("Item", XMLAttributes('I' AS "OP")
, XMLElement("ITN",1)
, MGCLI.F_XML_WITHOUT_ROOT()
)
) FROM DUAL
)
).extract('/*') indent
) XML_BL_XML
FROM DUAL
;
XML documents must have a single root element, else they are not well-formed, and compliant XML parsers are required to report such a problem as an error.
Fix your XML by wrapping the multiple elements in a single root element.