You are on page 1of 2

set SERVEROUTPUT ON;

DECLARE

lContext DBMS_XMLGEN.ctxHandle;
lHTMLOutput XMLTYPE;
xslt_tranfsorm XMLTYPE;
lXMLData XMLTYPE;
mailbody CLOB;
l_cursor SYS_REFCURSOR;
a number:=7;

-- SQL to get data:


BEGIN
open l_cursor for select *
from dept where deptno > a;

lContext := DBMS_XMLGEN.NEWCONTEXT(l_cursor);
lXMLData := DBMS_XMLGEN.GETXMLTYPE(lContext,DBMS_XMLGEN.NONE);

-- XSLT with HTML Template for Table :


xslt_tranfsorm := NEW XMLTYPE('
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/ROWSET">
<table>
<tr>
<th>DEPTNO</th>
<th>DNAME</th>
<th>LOC</th>
</tr>
<xsl:for-each select="ROW">
<tr>
<td> <xsl:value-of select="DEPTNO"/> </td>
<td> <xsl:value-of select="DNAME"/> </td>
<td> <xsl:value-of select="LOC"/> </td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>');

-- query_result := DBMS_XMLGEN.getxmltype(ctxh).transform(xslt_tranfsorm);

--mailbody := '<h3>The Result of the Query:</h3>' || Chr(13);


--Dbms_Lob.append(mailbody,query_result.getClobVal());
lHTMLOutput := lXMLData.transform(xslt_tranfsorm);
-- convert XMLType to Clob --
dbms_output.put_line(lHTMLOutput.getClobVal());

-- send_mail('your_sender@your_domain.com', 'your_recipient@your_domain.com' ,
'Automated Query', mailbody);
--DBMS_OUTPUT.PUT_LINE(l_xmltype.getClobVal);
close l_cursor;
exception
when others then
DBMS_OUTPUT.PUT_LINE('some error');
end;

You might also like