Remove spaces from HTML generated with XSL

Background

Maintain readable XSL source code while generating HTML without excessive breaks that introduce spaces between sentences and their punctuation interruption. From Rethinking XSLT :

White space in XSLT stylesheets is especially problematic because it serves two purposes: (1) to format the XSLT stylesheet itself; and (2) to indicate where the space should go in the output of the XML data processed by XSLT.

Problem

The XSL template contains the following code:

  <xsl:if test="@min-time &lt; @max-time">
    for
    <xsl:value-of select="@min-time" />
    to
    <xsl:value-of select="@max-time" />
    minutes
  </xsl:if>

  <xsl:if test="@setting">
    on <xsl:value-of select="@setting" /> heat
  </xsl:if>
  .

This, for example, generates the following output (with simple letters, as shown):

    for
    2
    to
    3
    minutes
  .

All major browsers produce:

for 2 to 3 minutes .

Almost flawless, with the exception of a space between the word minutesand punctuation. Desired Result:

for 2 to 3 minutes.

, , XSL, XSL.

, :

<xsl:value-of select="normalize-space($step)" />.

, <span> . <span> HTML-. :

<xsl:copy-of select="normalize-space($step)" />.

:

<xsl:strip-space elements="*" />
<xsl:output indent="no" ... />

XSLT- ?

!

+3
2

copy-of , . , .

:

<li><xsl:apply-templates select="$step" mode="nospace" />.</li>

:

<xsl:template match="text()" mode="nospace" priority="1" >
    <xsl:value-of select="normalize-space(.)" />
</xsl:template>

<xsl:template match="node() | @*" mode="nospace">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*" mode="nospace" />
    </xsl:copy>
</xsl:template>
+3

I. :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="t[@max-time > @min-time]">
  <span>
    <xsl:value-of select=
      "concat('for ', @min-time, ' to ', @max-time, ' minutes')"/>
  </span>
  <xsl:apply-templates select="@setting"/>
  <xsl:text>.</xsl:text>
 </xsl:template>

 <xsl:template match="@setting">
  <span>
    <xsl:value-of select="concat(' on ', ., ' heat')"/>
  </span>
 </xsl:template>
</xsl:stylesheet>

XML- ( !):

<t min-time="2" max-time="3" setting="moderate"/>

, :

<span>for 2 to 3 minutes</span>
<span> on moderate heat</span>.

, :

2 3   .

XML-:

<t min-time="2" max-time="3"/>

, :

<span>for 2 to 3 minutes</span>.

, :

2 - 3 .


II. () :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" xmlns:gen="gen:gen" xmlns:gen-attr="gen:gen-attr"
 exclude-result-prefixes="my gen">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <my:layout>
  <span>for <gen-attr:min-time/> to <gen-attr:max-time/> minutes</span>
  <gen-attr:setting><span> on <gen:current/> heat</span></gen-attr:setting>
  <gen:literal>.</gen:literal>
 </my:layout>

 <xsl:variable name="vLayout" select="document('')/*/my:layout/*"/>
 <xsl:variable name="vDoc" select="/"/>

 <xsl:template match="node()|@*">
   <xsl:param name="pCurrent"/>
     <xsl:copy>
       <xsl:apply-templates select="node()|@*">
       <xsl:with-param name="pCurrent" select="$pCurrent"/>
       </xsl:apply-templates>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:apply-templates select="$vLayout">
   <xsl:with-param name="pCurrent" select="$vDoc/*"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="gen-attr:*">
  <xsl:param name="pCurrent"/>
  <xsl:value-of select="$pCurrent/@*[name() = local-name(current())]"/>
 </xsl:template>

 <xsl:template match="gen-attr:setting">
  <xsl:param name="pCurrent"/>
  <xsl:variable name="vnextCurrent" select=
   "$pCurrent/@*[name() = local-name(current())]"/>
  <xsl:apply-templates select="node()[$vnextCurrent]">
    <xsl:with-param name="pCurrent" select="$vnextCurrent"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="gen:current">
  <xsl:param name="pCurrent"/>
   <xsl:value-of select="$pCurrent"/>
 </xsl:template>

 <xsl:template match="gen:literal">
  <xsl:apply-templates/>
 </xsl:template>
</xsl:stylesheet>

, () "" XML-.

. " ", - , XML.

+2

All Articles