Subject: Re: [xsl] Plain Text Handling in XSLT Output|
From: "Georg Hohmann" <georg.hohmann@xxxxxxxxx>
Date: Wed, 7 Jun 2006 12:23:32 +0200
Ups, thanks for all the answers! This is the the only mailinglist i know where you get replies in a nanosecond. ;-) Now i will chum up with the xsl:text element. Also i will try to replace my xsl:.if statements with the method mentioned by andrew. I think this will make my scripts much smaller.
On 6/7/06, Georg Hohmann <georg.hohmann@xxxxxxxxx> wrote: > Hello, > > i bother with the handling of plain text in xslt. My problem is that i > have to insert plain text combined with values to the output depending > on if a node exists or not (which is a very common task i guess). So > there are many xsl:if test=exists(...) statements inside my xslt. > > 1. Prob: White spaces > If i have a xslt like this: > ... > <xsl:value-of select="Firstname"/> <xsl:value-of select="Lastname"/> > ... > I always get this output: > ... > FirstnameLastname > ... > How do i insert a white space between (these) two values?
Whitespace only text nodes get dropped by default - that means any text node consisting soley of whitespace (like the whitespace used to indent your XSLT).
So with that in mind, use <xsl:text> </xsl:text> or <xsl:value-of select="concat(Firstname, ' ', Lastname)"/>
> 2. Prob: Line Feed & Carriage Return > The xslt: > ... > <person> > <xsl:if test="exists(mds:node17)"> > <name> > <xsl:value-of select="node17"/> > <xsl:if test="exists(node18)"> > (<xsl:value-of select="node18"/>) > </xsl:if> > </name> > </xsl:if> > </person> > ... > This is the expected output: > ... > Lastname (Function) > ... > But this is the real output: > ... > Lastname > (Function) > ... > How can i control if a linefeed is added or not? Is there a possibilty > to remove or add a linefeed with xslt to the output?
Here the text node contains a '(' so it doesn't get dropped, and the text node consists of the carriage return and indetation that you've used to indent your code - which is what your are seeing in your output. To avoid this write all the code inline, or use concat.
Also, rather than use xsl:test="exists(...)" just use apply-templates, as then if the element exists it will be processed, otherwise it wont:
<person> <xsl:apply-templates/> </person>
<xsl:template match="mds:node17"> <name> <xsl:value-of select="."/> <xsl:apply-templates/> </name> </xsl:template>
<xsl:template match="mds:node18"> <xsl:value-of select="concat('(', node18, ')'"/> <xsl:template>