简体   繁体   中英

Returning XML structures with SQL

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

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

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>

DEMO

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM