14 Replies Latest reply on May 20, 2019 3:35 PM by PK_UpNorth

    Empty Tags with Extra Whitespace

    PK_UpNorth

      I'm using XML DB on release12.1.0002. Specifically, I using a query with the xmlelement function to transform relational table data into XML.  The XML returned presents empty elements as self closing tags with whitespace before the '/'  as in "<ORDER_NOTES />".  The extra whitespace is problematic during subsequent processing.  Is there a setting or a way to remove it other than some sort of additional post processing?

       

      Thanks,

      Paul

        • 1. Re: Empty Tags with Extra Whitespace
          Jason_(A_Non)

          You have an example of what you are doing that demonstrates the issue?

           

          Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 
          
          SQL> SELECT XMLELEMENT("root", XMLELEMENT("ORDER_NOTES",NULL)) FROM dual;
          
          XMLELEMENT("ROOT",XMLELEMENT("ORDER_NOTES",NULL))
          --------------------------------------------------------------------------------
          <root><ORDER_NOTES></ORDER_NOTES></root>
          
          

           

          That works fine but is probably far different from what you are really doing.

          • 2. Re: Empty Tags with Extra Whitespace
            cormaco

            Whitespaces in empty tags are valid XML:

            https://www.w3.org/TR/xml/#dt-empty

            Tags for Empty Elements
            [44]   EmptyElemTag   ::=   '<' Name (S Attribute)* S? '/>'[WFC: Unique Att Spec]
            • 3. Re: Empty Tags with Extra Whitespace
              PK_UpNorth

              Jason,

                  Thanks for the reply.  Here is a more complete segment of the query.  If the C47TEXT query comes back null, then I get the self closing empty tag "<ORDER_NOTES />" output with the whitespace before the slash.

               

              xmlelement("ORDER_NOTES",
                              (SELECT xmlagg(xmlelement("Text", NVL(C47TEXT,'-')))
                                FROM (
                                      SELECT C47NID, C47DSEQ, C47TEXT
                                      FROM C47F, C41F
                                WHERE C47NID = C41NID
                                  AND C41ORDNO = C50TAB(j).C50ORDNO
                                  AND C41OLINE IS NULL
                                  AND C41NDST = 'B'
                                  ORDER BY C47NID, C47DSEQ
                              ))
                                      )
              

               

              Is there an option to output empty elements as either long form or short form without the extra whitespace?

               

              Thanks,

              Paul

              • 4. Re: Empty Tags with Extra Whitespace
                PK_UpNorth

                Thanks for the reply, cormaco.   True, whitespace in an empty tag may be acceptable XML, but does it need to be the default and only option?  Is there a way to output either a long form empty tag, e.g. <empty_tag></empty_tag> or the short form empty tag without the extra whitespace, e.g. <empty_tag/>?  I would prefer one of these other options.

                 

                Thanks,

                Paul

                • 5. Re: Empty Tags with Extra Whitespace
                  Jason_(A_Non)

                  I'm still not reproducing the format on the 12.1.0.2 I have access to

                  Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 
                  
                  SQL> 
                  SQL> SELECT XMLELEMENT("root",
                    2           XMLELEMENT("ORDER_NOTES",
                    3             (SELECT XMLAGG(XMLELEMENT("Text", nvl(r_val, '-')))
                    4                FROM (SELECT object_name r_val FROM user_objects WHERE object_name LIKE 'D3Q6%')))) rslt
                    5  FROM dual;
                  
                  RSLT
                  --------------------------------------------------------------------------------
                  <root><ORDER_NOTES></ORDER_NOTES></root>
                  
                  SQL> SELECT order_notes
                    2    FROM XMLTABLE('/root'
                    3                  PASSING XMLTYPE('<root><ORDER_NOTES /></root>')
                    4                  COLUMNS
                    5                  order_notes   VARCHAR2(5)  PATH 'ORDER_NOTES');
                  
                  ORDER_NOTES
                  -----------
                  
                  SQL> 
                  

                   

                  so I don't see it as being the default and only option.  No I don't know why your format is coming out different.  If either format is valid per W3C specs and Oracle can process either format clearly (as shown by second SQL above) and machines are processing the XML why does the format matter?

                  • 6. Re: Empty Tags with Extra Whitespace
                    cormaco
                    Thanks for the reply, cormaco. True, whitespace in an empty tag may be acceptable XML, but does it need to be the default and only option? Is there a way to output either a long form empty tag, e.g. <empty_tag></empty_tag> or the short form empty tag without the extra whitespace, e.g. <empty_tag/>? I would prefer one of these other options.

                    I don't think there are options for xmlelement, but you can try DBMS_XMLGEN:

                    Generation of XML Data Using DBMS_XMLGEN

                     

                    Look at this example on that page: Example 8-28 DBMS_XMLGEN: Specifying NULL Handling

                    1 person found this helpful
                    • 7. Re: Empty Tags with Extra Whitespace
                      PK_UpNorth

                      Thank you.  I've been looking at that functionality in DBMS_XMLGEN as an option.

                      • 8. Re: Empty Tags with Extra Whitespace
                        PK_UpNorth

                        The documents I'm producing with the query are for distribution outside of our organization.  I'm looking at providing the empty tag without the extra whitespace for consistency's sake to multiple recipients.

                         

                        Since we have different outputs under similiar input situations with the same database version, I have to wonder if this a a configuration option during database setup/upgrade.

                        • 9. Re: Empty Tags with Extra Whitespace
                          Jason_(A_Non)

                          Out of curiosity, does my example SQL statement produce the same output I showed or do you get the single empty tag setup for it as well?  Can you create the same empty tag setup in some SQL that can be shared and ran by others?

                           

                          I applaud you for trying to make the cleanest XML you can for distribution purposes.  One of the primary systems I maintain is both a receiver and producer of XML that flows between numerous systems (think hub and spoke type setup).  Multiple namespaces are involved just to make things worse.  I've seen all kinds of screwy looking XML (from the human readable perspective) that parse through both systems just fine on a daily basis because they are syntactically valid and meet all the business rules.  My suggestion is focus less on the addition of a single white space at the end of an empty tag and be happy you get that format instead of the <tag></tag> format my examples show.  Your output is smaller in terms of transmission format while being semantically equivalent.

                          • 10. Re: Empty Tags with Extra Whitespace
                            PK_UpNorth

                            This is odd behavior.  I ran your example SQL statement and got the long form empty tags, just as you did.  The segment of code I provided above, which is outputting the short form empty tag with the extra whitespace, is part of a larger XML query.  I pulled the segment out and ran it independently, it provided the long form empty tag output rather than the short form with the extra space.

                             

                            Additionally, the XML I was evaluating was the XML result output to the screen using DBMS_OUTPUT.  I have since modified the code to write the XML out to a file as well.  When looking at the results in the file, I get the short form empty tag result, but without the extra whitespace, which is what I'm looking for.

                             

                            Because I will ultimately be provideing the results as output written to a file, the issue appears to have resolved itself; however, I find the disparity in the 3 output results adds unnessecary confusion to the development process.

                             

                            Thanks for your assistance with this issue, Jason.

                             

                            Regards,

                            Paul

                            • 11. Re: Empty Tags with Extra Whitespace
                              Jason_(A_Non)

                              How were you converting the XML to a string for use with dbms_output and which approach are you using to write the XML to disk? 

                              • 12. Re: Empty Tags with Extra Whitespace
                                PK_UpNorth

                                For the DBMS_OUTPUT I'm doing:

                                MaxBuffSize = 32767;
                                SELECT XMLSERIALIZE(CONTENT FullDoc AS CLOB INDENT SIZE = 2) INTO ClobDoc FROM DUAL;
                                DocLength := DBMS_LOB.GETLENGTH(ClobDoc);
                                LoopVar := 1;
                                      LOOP
                                        DBMS_OUTPUT.PUT_LINE(DBMS_LOB.SUBSTR(ClobDoc,MaxBuffSize,LoopVar));
                                        LoopVar := LoopVar+MaxBuffSize;
                                        EXIT WHEN LoopVar > DocLength;
                                      END LOOP;
                                

                                 

                                For the file write I'm doing, where Clobdoc is the ClobDoc from the XMLSERIALIZE above:

                                DBMS_XSLPROCESSOR.clob2file(ClobDoc,'OUT_DIR',FileNameOut);
                                

                                 

                                Thanks,

                                Paul

                                • 13. Re: Empty Tags with Extra Whitespace
                                  Jason_(A_Non)

                                  Your approaches seem sound to me.  Something in the back of my head says there is a conflict between the indenting and writing that clob out via dbms_output, but then it also could be some pretty print issues that occurred in 11g or before.  Doesn't explain the short form in the file itself though.

                                   

                                  Happy coding.

                                  • 14. Re: Empty Tags with Extra Whitespace
                                    PK_UpNorth

                                    Thank you for your help and input on this, Jason.