Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove whitespace when declaring an XSL variable?

Tags:

xslt

I have to create an XSL variable with a choose in it. Like the following:

<xsl:variable name="grid_position">
  <xsl:choose>
    <xsl:when test="count(/Element) &gt;= 1">
      inside
    </xsl:when>
    <xsl:otherwise>
      outside
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

And later in my code, I do an xsl if:

<xsl:if test="$grid_position = 'inside'">
   {...code...}
</xsl:if>

Problem is that my variable is never = 'inside' because of the line breaks and indent. How can I remove whitespaces from my variable? I know I can remove it using disable-output-escaping="yes" when I use it in a xsl:copy-of, but it's not working on the xsl:variable tag. So how can I remove those whitespace and line breaks?

like image 512
Gabriel Avatar asked Sep 03 '10 15:09

Gabriel


People also ask

How do I remove spaces in XSLT?

XSLT <xsl:strip-space> The <xsl:strip-space> element is used to define the elements for which white space should be removed. Note: Preserving white space is the default setting, so using the <xsl:preserve-space> element is only necessary if the <xsl:strip-space> element is used.

How do you define an XSL variable?

Definition of XSLT Variable. XSLT variable is defined as special tags used to declare a local or global variable that we make use of to store any values. The declared variables are referenced within an Xpath expression. Once it is set we cannot overwrite or update the variables.

How do I trim a string in XSLT?

Either you could use normalize-space function, which returns a string whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space within the string, or you can try to implement a template that recursively removes white spaces from the beginning ...


2 Answers

The strategies in the other answers are good, in fact preferable to this one when feasible. But there are times when you don't have control over (or it's harder to control) what's in the variable. In those cases, you can strip away the surrounding space when you're testing the variable:

Instead of

<xsl:if test="$grid_position = 'inside'">

use

<xsl:if test="normalize-space($grid_position) = 'inside'">

normalize-space() strips the leading and trailing whitespace, and collapses other repeating white spaces to single ones.

like image 105
LarsH Avatar answered Oct 07 '22 04:10

LarsH


That's what <xsl:text> is for:

<xsl:variable name="grid_position">
  <xsl:choose>
    <xsl:when test="count(/Element) &gt;= 1">
      <xsl:text>inside</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>outside</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

It allows you to structure your code and control whitespace at the same time.

In fact, you should stay clear of text nodes in XSL that are not wrapped in <xsl:text> to avoid these kinds of bugs in the future, too (i.e. when code gets re-formatted or re-factored later).

For simple cases, like in your sample, doing what Jim Garrison suggests is also an option.


As an aside, testing for the existence of an element with count() is superfluous. Selecting it is enough, since the empty node-set evaluates to false.

<xsl:when test="/Element">
like image 30
Tomalak Avatar answered Oct 07 '22 03:10

Tomalak