1 Reply Latest reply: Dec 20, 2012 3:58 AM by odie_63 RSS

    How to force empty element short form?

    TPD-Opitz
      Hello,

      I'd like to crete an XML with most data stored in attributes:
      SQL*Plus: Release 11.2.0.1.0 Production on Do Dez 20 10:18:43 2012
      
      Copyright (c) 1982, 2010, Oracle.  All rights reserved.
      
      Verbunden mit:
      Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
      With the Partitioning, Automatic Storage Management and OLAP options
      SQL> WITH my_data AS
        2         (SELECT 'test1' AS element_name
        3               , 0 AS tag_attrib
        4            FROM DUAL
        5          UNION ALL
        6          SELECT 'test2' AS element_name
        7               , 1 AS tag_attrib
        8            FROM DUAL)
        9  SELECT XMLELEMENT( element_name )
       10       , XMLELEMENT( element_name
       11                   , xmlattributes( tag_attrib as "tag_attrib" )
       12                    )
       13    FROM my_data;
      
      XMLELEMENT(ELEMENT_NAME)
      --------------------------------------------------------------------
      XMLELEMENT(ELEMENT_NAME,XMLATTRIBUTES(TAG_ATTRIBAS"TAG_ATTRIB"))
      --------------------------------------------------------------------
      <ELEMENT_NAME></ELEMENT_NAME>
      <ELEMENT_NAME tag_attrib="0"></ELEMENT_NAME>
      
      <ELEMENT_NAME></ELEMENT_NAME>
      <ELEMENT_NAME tag_attrib="1"></ELEMENT_NAME>
      how do I force the result beeing like this:
      XMLELEMENT(ELEMENT_NAME)
      --------------------------------------------------------------------
      XMLELEMENT(ELEMENT_NAME,XMLATTRIBUTES(TAG_ATTRIBAS"TAG_ATTRIB"))
      --------------------------------------------------------------------
      <ELEMENT_NAME/>
      <ELEMENT_NAME tag_attrib="0"/>
      
      <ELEMENT_NAME/>
      <ELEMENT_NAME tag_attrib="1"/>
      bye
      TPD
        • 1. Re: How to force empty element short form?
          odie_63
          Hi,

          Presenting elements like this is controlled via serialization options.

          In 11.2, you can use XMLSerialize function with INDENT option :
          SQL> WITH my_data(element_name, tag_attrib) AS (
            2    SELECT 'test1', 0 FROM DUAL UNION ALL
            3    SELECT 'test2', 1 FROM DUAL
            4  )
            5  SELECT XMLSerialize(document
            6           XMLElement( element_name
            7           , XMLAttributes(tag_attrib as "tag_attrib")
            8           )
            9           indent size = 0
           10         )
           11  FROM my_data;
           
          XMLSERIALIZE(DOCUMENTXMLELEMEN
          --------------------------------------------------------------------------------
          <ELEMENT_NAME tag_attrib="0"/>
          <ELEMENT_NAME tag_attrib="1"/>
           
          The function serializes as CLOB by default.
          You can specify VARCHAR2 or BLOB too.

          http://docs.oracle.com/cd/E11882_01/appdev.112/e23094/xdb13gen.htm#ADXDB5033