This is the mail archive of the docbook-apps@lists.oasis-open.org mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: [docbook-apps] trim space off tag contents


We are using the following piece of code for this. It's just for screens but
you can change it to work on program listings as well.

Peter


<!-- normalized screens -->

<xsl:template match="screen/text()">
  <xsl:variable name="before" select="preceding-sibling::node()"/>
  <xsl:variable name="after" select="following-sibling::node()"/>

  <xsl:variable name="conts" select="."/>

  <xsl:variable name="contsl">
    <xsl:choose>
      <xsl:when test="count($before) = 0">
	<xsl:call-template name="remove-lf-left">
          <xsl:with-param name="astr" select="$conts"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$conts"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="contslr">
    <xsl:choose>
      <xsl:when test="count($after) = 0">
        <xsl:call-template name="remove-ws-right">
          <xsl:with-param name="astr" select="$contsl"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$contsl"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:value-of select="$contslr"/>

</xsl:template>


<!-- eats linefeeds from the left -->
<xsl:template name="remove-lf-left">
  <xsl:param name="astr"/>

  <xsl:choose>
    <xsl:when test="starts-with($astr,'&#xA;') or
                    starts-with($astr,'&#xD;')">
      <xsl:call-template name="remove-lf-left">
        <xsl:with-param name="astr" select="substring($astr, 2)"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$astr"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- eats whitespace from the right -->
<xsl:template name="remove-ws-right">
  <xsl:param name="astr"/>

  <xsl:variable name="last-char">
    <xsl:value-of select="substring($astr, string-length($astr), 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="remove-ws-right">
        <xsl:with-param name="astr"
          select="substring($astr, 1, string-length($astr) - 1)"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$astr"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


> -----Original Message-----
> From: Gabor Hojtsy [mailto:gabor at hojtsy dot hu]
> Sent: Sonntag, 27. April 2003 12:51
> To: Dave Pawson
> Cc: docbook-apps at lists dot oasis-open dot org
> Subject: Re: [docbook-apps] trim space off tag contents
>
>
> > >The normalize-space() function in XSL removes leading
> > >and trailing spaces, and replaces internal sequences
> > >of whitespace with a single space character.
> >
> > If its many elements, then an identity transform with
> > <xsl:strip-space elements="*"/>
> > does the same job.
> > Replace * with as many elements as needed
> > or use...
> > <xsl:preserve-space elements="pre"/> or whatever.
> > Can be used in combination.
>
> This does not solve our problems. Here is a fragment:
>
> <programlisting role="php">
> <![CDATA[
> <html>
>  <head>
>   <title>PHP Test</title>
>  </head>
>  <body>
>  <?php echo "<p>Hello World</p>"; ?>
>  </body>
> </html>
> ]]>
> </programlisting>
>
> This will effectively output a code with the leading spaces (the newlines
> after the programlisting tag and after the <![CDATA[ tag)
> present, and with
> the trailing newlines also present. We would not like to put the
> <![CDATA[
> and the programlisting and the first line of the example on the
> same line to
> avoid this, for readability reasons. But we would not like to get those
> newlines on the output.
>
> <xsl:strip-space> is to strip whitespace-only text nodes, AFAIK,
> which is not
> the case here. normalize-space() will remove any multiple spaces,
> which will
> highly distract the formatting of the example above. So none of these are
> solutions. As I said, we would like to strip of the leading and trailing
> spaces, and nothing else. Others, using <programlisting> must
> have been faced
> the same problem, I guess...
>
> Thanks,
> Goba
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: docbook-apps-unsubscribe at lists dot oasis-open dot org
> For additional commands, e-mail: docbook-apps-help at lists dot oasis-open dot org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: docbook-apps-unsubscribe at lists dot oasis-open dot org
For additional commands, e-mail: docbook-apps-help at lists dot oasis-open dot org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]