]> granicus.if.org Git - docbook-dsssl/commitdiff
Added a utility template for trimming whitespace off the beginning
authorMichael Smith <xmldoc@users.sourceforge.net>
Thu, 20 Oct 2005 13:49:41 +0000 (13:49 +0000)
committerMichael Smith <xmldoc@users.sourceforge.net>
Thu, 20 Oct 2005 13:49:41 +0000 (13:49 +0000)
and end of text chunks.

xsl/manpages/utility.xsl

index c8efaf39eda6d2e6f31d10d71356fd99c142d638..a50b574f0bcb95c94f748692f70b27a83afd9032 100644 (file)
     </xsl:if>
   </xsl:template>
 
+  <!-- ================================================================== -->
+
+  <xsl:template match="*" mode="trim">
+    <xsl:param name="contents" select="."/>
+    <xsl:variable name="contents-left-trimmed">
+      <xsl:call-template name="trim-left">
+        <xsl:with-param name="contents" select="$contents"/>
+      </xsl:call-template>
+    </xsl:variable>
+    <xsl:variable name="contents-trimmed">
+      <xsl:call-template name="trim-right">
+        <xsl:with-param name="contents" select="$contents-left-trimmed"/>
+      </xsl:call-template>
+    </xsl:variable>
+    <xsl:value-of select="$contents-trimmed"/>
+  </xsl:template>
+
+  <xsl:template name="trim-left">
+    <xsl:param name="contents"/>
+    <xsl:choose>
+      <xsl:when test="starts-with($contents,'&#xA;') or
+                      starts-with($contents,'&#xA;') or
+                      starts-with($contents,'&#x20;') or
+                      starts-with($contents,'&#x9;')">
+        <xsl:call-template name="trim-left">
+          <xsl:with-param name="contents" select="substring($contents, 2)"/>
+        </xsl:call-template>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:value-of select="$contents"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template name="trim-right">
+    <xsl:param name="contents"/>
+    <xsl:variable name="last-char">
+      <xsl:value-of select="substring($contents, string-length($contents), 1)"/>
+    </xsl:variable>
+    <xsl:choose>
+      <xsl:when test="($last-char = '&#xA;') or
+                      ($last-char = '&#xD;') or
+                      ($last-char = '&#x20;') or
+                      ($last-char = '&#x9;')">
+        <xsl:call-template name="trim-right">
+          <xsl:with-param name="contents"
+                          select="substring($contents, 1, string-length($contents) - 1)"/>
+        </xsl:call-template>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:value-of select="$contents"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
 </xsl:stylesheet>