12

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?

1
  • Good question (+1). See my answer for a short, one-liner XPath expression for your <xsl:variable> :) Commented Sep 3, 2010 at 16:19

4 Answers 4

18

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">
1
  • ya this works well even for <xsl:variable name="Params"> <xsl:for-each select="paramlists/list/param"> <xsl:value-of select="@name"/><xsl:text>:</xsl:text><xsl:value-of select="@value"/><xsl:text>;</xsl:text> </xsl:for-each> </xsl:variable>
    – zulucoda
    Commented May 3, 2012 at 10:18
5

The simplest way is not to put the whitespace there in the first place:

<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>
2
  • 2
    Thank you for your answer, but I went with Tomalak's answer because I don't want my code to break if someone reformat the xsl code to add a line break.
    – Gabriel
    Commented Sep 3, 2010 at 17:34
  • 1
    @Gabriel: You did the right thing. Tomalak's answer is more general. Commented Sep 3, 2010 at 19:19
5

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.

1
  • 1
    +1 That's the obvious answer that has been missing so far. Storing surplus whitespace and then stripping it again is not elegant, but sometimes there is no way around it, e.g. when you have no influence on the XML.
    – Tomalak
    Commented Sep 5, 2010 at 10:51
1

Just use:

<xsl:variable name="grid_position" select=
 "concat(substring('inside', 1 div boolean(/Element)),
         substring('outside', 1 div not(/Element))
        )
 "/>
1
  • +1 for this great tip. I often miss the "? :" ternary operator of C/Java/JavaScript.
    – dolmen
    Commented Sep 9, 2010 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.