[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]
Subject: AW: [docbook-apps] Programlisting strip-spaces problem
The following should work (I'm using it for 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,'
') or starts-with($astr,'
')"> <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 = '
') or ($last-char = '
') or ($last-char = ' ') or ($last-char = '	')"> <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> Regards Peter > -----Ursprüngliche Nachricht----- > Von: Bob Stayton [mailto:bobs@sagehill.net] > Gesendet: Mittwoch, 6. Oktober 2004 08:19 > An: TiP; docbook-apps@lists.oasis-open.org > Betreff: Re: [docbook-apps] Programlisting strip-spaces problem > > The basic problem is that your current template converts the > element to a > string. When you convert an element to a string in XSLT, the > child elements > are converted to strings too. So your first substring > expressions do the > reduction. > > You'll need to handle text() nodes and element nodes > differently, so you can > process the elements as elements. I don't have an > off-the-shelf solution > for what you want to do, but maybe someone else does. > > Bob Stayton > Sagehill Enterprises > DocBook Consulting > bobs@sagehill.net > > > ----- Original Message ----- > From: "TiP" <TiP@tut.by> > To: <docbook-apps@lists.oasis-open.org> > Sent: Friday, October 01, 2004 12:20 AM > Subject: [docbook-apps] Programlisting strip-spaces problem > > > > Hello docbook-apps, > > > > I've made some customizations to progamlisting to strip > empty lines > > in the beginning and in the end of it. It is a recursive > template, but > > this is not a problem. The problem is that all internal > formatting of > > programlisting is lost. For example: > > > > <programlisting> > > $ cvs > -d:pserver:<parameter>username</parameter>@cvs.site.net:/repository > checkout <parameter>module</parameter> > > </programlisting> > > > > Template below produces output string by string, omitting newlines > > at the end and beginning. And somewhere during conversion internal > > formatting for these strings is lost. Like > > $ cvs -d:pserver:<TT>username</TT>@cvs.site.net:/repository checkout > <TT>module</TT> > > becomes > > $ cvs -d:pserver:username@cvs.site.net:/repository checkout module > > is there any way to avoid losing markup information? > > Maybe some exslt functions? > > > > <xsl:template name="trim_newlines"> > > <xsl:param name="string" select="."/> > > <xsl:param name="in_end_mode" select="false()"/> <!-- > looking for > endstring --> > > <xsl:message> > > <xsl:copy-of select="$string"/> > > <xsl:text> </xsl:text> > > </xsl:message> > > > > <xsl:if test="normalize-space($string)"> <!-- > prevent endless > cycle on empty blocks --> > > <xsl:variable name="nl" select="'
'" /> > > <xsl:variable name="beforenl" > select="substring-before($string,$nl)" > /> > > <xsl:variable name="afternl" > select="substring-after($string,$nl)" > /> > > <xsl:variable name="nextnl" > select="normalize-space(substring-before($afternl,$nl))" /> > > <xsl:choose> > > <xsl:when test="not($in_end_mode) and > string-length(normalize-space($beforenl)) = 0"> > > <xsl:call-template name="trim_newlines"> > > <xsl:with-param name="string" select="$afternl" /> > > <xsl:with-param name="in_end_mode" > select="$in_end_mode or > $nextnl" /> > > </xsl:call-template> > > </xsl:when> > > <xsl:otherwise> > > <xsl:copy-of select="concat($beforenl,$nl)"/> > > <xsl:call-template name="trim_newlines"> > > <xsl:with-param name="string" select="$afternl" /> > > <xsl:with-param name="in_end_mode" select="true()" /> > > </xsl:call-template> > > </xsl:otherwise> > > </xsl:choose> > > </xsl:if> > > </xsl:template> > > > > Tnx. > > -- > > TiP > > > > > > > >
[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]