chardex
chardex

Reputation: 323

Saxon XSLT 2.0 ignore whitespace

I'm using text output method. And I need to ignore all whitespaces in template.

<xsl:template ...>
   text
</xsl:template>

I'm receiving in output "     text", but I need only "text".

Thanks.

Upvotes: 1

Views: 473

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243539

<xsl:template ...>   
   text   
</xsl:template>   

I'm receiving in output " text", but I need only "text".

Use:

<xsl:template ...> 
   <xsl:text>text</xsl:text> 
</xsl:template>

Explanation: In XSLTAny node that is not white-space-only, doesn't belong to the "xsl namespace" and is a child of an <xsl:template> is output "as-is". The XSLT Spec specifically says:

"A template can also contain text nodes. Each text node in a template remaining after whitespace has been stripped as specified in [3.4 Whitespace Stripping] will create a text node with the same string-value in the result tree"

The <xsl:text> instruction was designed exactly with this use-case in mind. It gives the developer control to specify exactly what text should be output.

Upvotes: 6

Related Questions