--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>\r
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">\r
+<!-- ********************************************************************\r
+ $Id$\r
+ ********************************************************************\r
+ Copyright 2008, Regina Obe\r
+ License: BSD\r
+ Purpose: This is an xsl transform that generates PostgreSQL COMMENT ON FUNCTION ddl\r
+ statements from postgis xml doc reference\r
+ ******************************************************************** -->\r
+ <xsl:output method="text" />\r
+\r
+ <!-- We deal only with the reference chapter -->\r
+ <xsl:template match="/">\r
+ <xsl:apply-templates select="/book/chapter[@id='RT_reference']" />\r
+ </xsl:template>\r
+\r
+ <xsl:template match="chapter">\r
+ <xsl:variable name="ap"><xsl:text>'</xsl:text></xsl:variable>\r
+<!-- Pull out the purpose section for each ref entry and strip whitespace and put in a variable to be tagged unto each function comment -->\r
+ <xsl:for-each select="sect1[not(contains(@id,'Operator'))]/refentry">\r
+ <xsl:variable name='plaincomment'>\r
+ <xsl:value-of select="normalize-space(translate(translate(refnamediv/refpurpose,'
', ' '), '	', ' '))"/>\r
+ </xsl:variable>\r
+<!-- Replace apostrophes with 2 apostrophes needed for escaping in SQL -->\r
+ <xsl:variable name='comment'>\r
+ <xsl:call-template name="globalReplace">\r
+ <xsl:with-param name="outputString" select="$plaincomment"/>\r
+ <xsl:with-param name="target" select="$ap"/>\r
+ <xsl:with-param name="replacement" select="''"/>\r
+ </xsl:call-template>\r
+ </xsl:variable>\r
+ <xsl:choose>\r
+<!-- If this is a postgis type grab the ref entry summary and refname to make type comment -->\r
+<xsl:when test="parent::sect1[@id='PostGIS_Types']">\r
+ COMMENT ON TYPE <xsl:value-of select="refnamediv/refname" /> IS 'postgis type: <xsl:value-of select='$comment' />';\r
+</xsl:when>\r
+ </xsl:choose>\r
+<!-- For each function prototype generate the DDL comment statement\r
+ If its input is a geometry set - we know it is an aggregate function rather than a regular function -->\r
+ <xsl:for-each select="refsynopsisdiv/funcsynopsis/funcprototype">\r
+COMMENT ON <xsl:choose><xsl:when test="contains(paramdef/type,'geometry set')">AGGREGATE</xsl:when><xsl:otherwise>FUNCTION</xsl:otherwise></xsl:choose><xsl:text> </xsl:text> <xsl:value-of select="funcdef/function" />(<xsl:for-each select="paramdef"><xsl:choose><xsl:when test="count(parameter) > 0"> \r
+<xsl:choose><xsl:when test="contains(type,'geometry set')">geometry</xsl:when><xsl:otherwise><xsl:value-of select="type" /></xsl:otherwise></xsl:choose><xsl:if test="position()<last()"><xsl:text>, </xsl:text></xsl:if></xsl:when>\r
+</xsl:choose></xsl:for-each>) IS '<xsl:call-template name="listparams"><xsl:with-param name="func" select="." /></xsl:call-template> <xsl:value-of select='$comment' />';\r
+ </xsl:for-each>\r
+ </xsl:for-each>\r
+ </xsl:template>\r
+ \r
+<!--General replace macro hack to make up for the fact xsl 1.0 does not have a built in one. \r
+ Not needed for xsl 2.0 lifted from http://www.xml.com/pub/a/2002/06/05/transforming.html -->\r
+ <xsl:template name="globalReplace">\r
+ <xsl:param name="outputString"/>\r
+ <xsl:param name="target"/>\r
+ <xsl:param name="replacement"/>\r
+ <xsl:choose>\r
+ <xsl:when test="contains($outputString,$target)">\r
+ <xsl:value-of select=\r
+ "concat(substring-before($outputString,$target),\r
+ $replacement)"/>\r
+ <xsl:call-template name="globalReplace">\r
+ <xsl:with-param name="outputString" \r
+ select="substring-after($outputString,$target)"/>\r
+ <xsl:with-param name="target" select="$target"/>\r
+ <xsl:with-param name="replacement" \r
+ select="$replacement"/>\r
+ </xsl:call-template>\r
+ </xsl:when>\r
+ <xsl:otherwise>\r
+ <xsl:value-of select="$outputString"/>\r
+ </xsl:otherwise>\r
+ </xsl:choose>\r
+ </xsl:template>\r
+\r
+ <!--macro to pull out function parameter names so we can provide a pretty arg list prefix for each function -->\r
+ <xsl:template name="listparams">\r
+ <xsl:param name="func" />\r
+ <xsl:for-each select="$func">\r
+ <xsl:if test="count(paramdef/parameter) > 0">args: </xsl:if>\r
+ <xsl:for-each select="paramdef">\r
+ <xsl:choose>\r
+ <xsl:when test="count(parameter) > 0"> \r
+ <xsl:value-of select="parameter" />\r
+ </xsl:when>\r
+ </xsl:choose>\r
+ <xsl:if test="position()<last()"><xsl:text>, </xsl:text></xsl:if>\r
+ </xsl:for-each>\r
+ <xsl:if test="count(paramdef/parameter) > 0"> - </xsl:if>\r
+ </xsl:for-each> \r
+ </xsl:template>\r
+\r
+</xsl:stylesheet>\r