Reputation: 3965
In an XSLT stylesheet, how can I remove leading and trailing whitespace inside a <xsl:attribute>
tag?
For example, the following stylesheet:
<xsl:template match="/">
<xsl:element name="myelement">
<xsl:attribute name="myattribute">
attribute value
</xsl:attribute>
</xsl:element>
</xsl:template>
outputs:
<myelement myattribute=" attribute value "/>
whilst I would like it to output:
<myelement myattribute="attribute value"/>
Is there any way to accomplish that other than collapsing the <xsl:attribute>
start and end tags in a single line?
Because if the attribute value is not a plain line of text but the result of some complex calculation (for example using or tags), then collapsing all the code in one line to avoid the leading and trailing whitespace would result in a horribly ugly stylesheet.
Upvotes: 6
Views: 1086
Reputation: 14800
You could wrap the text by xsl:text or xsl:value-of:
<xsl:template match="/">
<xsl:element name="myelement">
<xsl:attribute name="myattribute">
<xsl:text>attribute value</xsl:text>
</xsl:attribute>
</xsl:element>
</xsl:template>
or
<xsl:template match="/">
<xsl:element name="myelement">
<xsl:attribute name="myattribute">
<xsl:value-of select="'attribute value'"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
Is this useful for you? Otherwise please explain your problem in using a single line.
Please take notice of the comment of Michael Kay, it explains the problem to the point!
Upvotes: 7