Skip to content

Instantly share code, notes, and snippets.

@erochest
Created December 19, 2012 20:55
Show Gist options
  • Save erochest/4340342 to your computer and use it in GitHub Desktop.
Save erochest/4340342 to your computer and use it in GitHub Desktop.
More unholy SQL tricks. This time with Postgres.
CREATE EXTENSION xml2;
-- Credit where credit's due. Or blame:
-- http://dba.publicexchange.com/a/8188
create or replace function bytea_import(p_path text, p_result out bytea)
language plpgsql as $$
declare
l_oid oid;
r record;
begin
p_result := '';
select lo_import(p_path) into l_oid;
for r in ( select data
from pg_largeobject
where loid = l_oid
order by pageno ) loop
p_result = p_result || r.data;
end loop;
perform lo_unlink(l_oid);
end;$$;
UPDATE metadata
SET data=xslt_process(data, convert_from(bytea_import('/Users/err8n/p/geoserver2/ch-data-stores/metadata.xslt'), 'utf8')::text, '')
WHERE data LIKE '%lat.lib.virginia.edu%';
drop function if exists bytea_import(text);
DROP EXTENSION xml2;
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:gmd="http://www.isotc211.org/2005/gmd">
<xsl:variable name='src' select="'lat.lib.virginia.edu'" />
<xsl:variable name='target' select ="'libsvr35.lib.virginia.edu'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//gmd:URL">
<xsl:element name="{name(.)}">
<xsl:choose>
<xsl:when test="contains(string(.), $src)">
<xsl:value-of select="concat(substring-before(string(.), $src), $target, substring-after(string(.), $src))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="string(.)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment