2 Replies Latest reply on Dec 7, 2018 12:22 PM by cormaco

    How to define namespace when concatenating XmlType from various functions?

    happybuddha

      I am generating an xml file from a procedure on Oracle 11. This procedure calls multiple functions to get various parts of the xml. Then finally, all of these little fragments are clubbed together and the final xml is spit out. It looks something like this :

       

      select 
            xmlserialize(DOCUMENT
      xmlelement("def:SomeReport",
      xmlattributes('http://bbc.uk.au/comn/service.service'   as "xmlns:def",  
            'http://www.w3.org/2005/05/xmlmime'   as "xmlns:xmime",
            'http://www.w3.org/2001/XMLSchema-instance' as "xmlns:xsi",
            'http://www.who.is.me/blah' as "xmlns:abc")
      ,
      xmlelement("StdMsg, 
      xmlconcat(
      v_frag1_xml,
      v_frag2_xml)
      )
        ) as CLOB VERSION '1.0" encoding="UTF-8' INDENT SIZE = 2
      )
      into final_xml  --   final_xml out CLOB
      from dual;
      

       

      So everything was working well till it was decided the fragments of xml should be namespaced with `abc`

       

      Now, when I run this procedure, where it generates v_frag1_xml (which in turn calls about 10 other functions to get smaller fragments, I get a

      Error occurred in XML processing LPX-00234: namespace prefix "abc" is not declared Error at line 1 ORA-06512: 
      

       

      The functions returning one of the fragments looks like so

       

      select xmlconcat(v_business_1_xml, 
      xmlelement("BlahId", 
      xmlelement("BlahNumber",v_index), 
      xmlelement("BlahText", 
      xmlelement("abc:CursedTag",xmlconcat(
      v_another_xml,
      v_furtherAnother_xml
      )
      
      )
      into frag1_xml
      from dual
      

      ;

       

      How do I go about generating smaller fragments of XML from the procedures without running into the

       

      Error occurred in XML processing LPX-00234: namespace prefix "abc" is not declared Error at line 1 ORA-06512: 
      

       

       

      Cheers

       

      Message was edited by: happybuddha. Updated oracle version

        • 1. Re: How to define namespace when concatenating XmlType from various functions?
          mNem

          Works for me ... tested on sqlfiddle.com.

           

          select
          xmlserialize (document 
             xmlelement("def:SomeReport"
                         , xmlattributes(
                              'http://bbc.uk.au/comn/service.service' as "xmlns:def"
                            , 'http://www.w3.org/2005/05/xmlmime' as "xmlns:xmime"
                            , 'http://www.w3.org/2001/XMLSchema-instance' as "xmlns:xsi"
                            , 'http://www.who.is.me/blah' as "xmlns:abc"
                         )
                         , 
                         xmlelement("StdMsg", xmlconcat(xmlelement("abc:a", 'Hello')
                                                      , xmlelement("b", 'World!')
                                                      , xmlelement("abc:CursedTag",xmlconcat(  
                                                                                              xmlelement("c", 'Hi,')
                                                                                            , xmlelement("abc:d", 'Again...')  
                                                                                            ) 
                                                                  )
                                                       )
                                                                  
                                   )  
             ) 
             as CLOB VERSION '1.0" encoding = "UTF-8' INDENT SIZE = 2  
          )  
           final_xml   
          from dual;
          

           

           

          FINAL_XML                                                                       
          --------------------------------------------------------------------------------
          <?xml version="1.0" encoding = "UTF-8"?>
          <def:SomeReport xmlns:def="http://bbc.uk.au/comn/service.service" xmlns:xmime="h
          ttp://www.w3.org/2005/05/xmlmime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-in
          stance" xmlns:abc="http://www.who.is.me/blah">
            <StdMsg>
              <abc:a>Hello</abc:a>
              <b>World!</b>
              <abc:CursedTag>
                <c>Hi,</c>
                <abc:d>Again...</abc:d>
              </abc:CursedTag>
            </StdMsg>
          </def:SomeReport>
          
          • 2. Re: How to define namespace when concatenating XmlType from various functions?
            cormaco

            I can reproduce the error on Oracle 11.2.0.3.0 when I use xmlserialize with indent. Without indent there is no error.

            You can avoid the error if you declare the missing namespace in the tag itself:

            SELECT 
                XMLSERIALIZE(CONTENT XMLELEMENT("abc:CursedTag",XMLATTRIBUTES('http://www.who.is.me/blah' AS "xmlns:abc"),'Somevalue') AS varchar2(100) INDENT) AS xmltest
            FROM dual;
            
            

             

            XMLTEST                                                                         
            --------------------------------------------------------------------------------
            <abc:CursedTag xmlns:abc="http://www.who.is.me/blah">Somevalue</abc:CursedTag>
                                                                                           
            1 row selected.