title: How to Convert Output of listagg() function into XMLTYPE
body: <p>I have one stored Procedure <code>P_FP_GET_PATTERN</code>.I expect output in following format </p>
  
  <pre><code>PATTERN_ID |PATTERN_NAME     | SHIFT
  1           Pattern 1         A,B,C,B,A
  2           Pattern 2         C,B,A,C
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/hsZ3p.png" alt="OUTPUT OF QUERY"></p>
  
  <p>I'm getting this output in SYS_REFCURSOR <code>ALL_RESULT_SET</code>.I need to pass it in XML format.But the moment i put that output in <code>ALL_RESULT_SET_XML</code> which is my out parameter of stored procedure of XMLTYPE using <code>ALL_RESULT_SET_XML:= XMLTYPE(ALL_RESULT_SET);</code> 
  Im getting an error as </p>
  
  <pre><code>Error encountered: ORA-31061: XDB error: special char to escaped char conversion failed.
  </code></pre>
  
  <p>I`m getting this error due to column shown using LISTAGG() function.Anybody can please tell me how to handle this ?</p>
  
  <p>My stored Procedure</p>
  
  <pre><code>create or replace
  PROCEDURE           P_FP_GET_PATTERN
  (
      ALL_RESULT_SET_XML  OUT XMLTYPE,
      P_MESSAGE_ALL   OUT VARCHAR2
  )
  AS
  V_ERROR  VARCHAR2(2000);
  ALL_RESULT_SET SYS_REFCURSOR;
  BEGIN 
  OPEN ALL_RESULT_SET FOR
  SELECT PM.PATTERN_ID ,PM.PATTERN_NAME,
   LISTAGG(SM.SHIFT_NUMBER) WITHIN GROUP (ORDER BY PD.INSTANCE_DAY) "SHIFT"
    FROM T_FP_PATTERN_MASTER PM,
     T_FP_PATTERN_DETAILS PD, 
     T_FP_SHIFT_MASTER SM
   WHERE SM.SHIFT_ID= PD.SHIFT_ID 
     AND PM.PATTERN_ID = PD.PATTERN_ID
  GROUP BY PM.PATTERN_NAME,PM.PATTERN_ID;
  
  --Adding output in XML output parameter
  ALL_RESULT_SET_XML:= XMLTYPE(ALL_RESULT_SET);
  EXCEPTION
  WHEN OTHERS
  THEN
      V_ERROR := SUBSTR(SQLERRM,1,1000);
      P_MESSAGE_ALL  := 'Error encountered: '||V_ERROR ;
  
  END;
  </code></pre>