PostgreSQL - XML Pretty Function
drop function if exists fnc_xml_pretty(pXML xml) cascade;
create or replace function fnc_xml_pretty(pXML xml) returns xml as
$body$
declare
vRetorno xml;
begin
vRetorno := xslt_process(pXML::text, '
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
$body$
declare
vRetorno xml;
begin
vRetorno := xslt_process(pXML::text, '
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
');
return vRetorno;
</xsl:template>
</xsl:stylesheet>
');
return vRetorno;
end;
$body$
language 'plpgsql' immutable strict;
--select fnc_xml_pretty('<root><a><b>some text</b></a></root>');
$body$
language 'plpgsql' immutable strict;
--select fnc_xml_pretty('<root><a><b>some text</b></a></root>');
--Reference: https://postgres.cz/wiki/PostgreSQL_SQL_Tricks#Pretty_xml_formating