lz430
lz430

Reputation: 135

XSLT conditional variable

I have a relatively simple problem. (At least I think it's simple).

I'm pulling values from a SharePoint 2013 list. I have the following code:

<xsl:variable name="target">
  <xsl:choose>
    <xsl:when test="@NewWindow = 'Yes'">
      <xsl:value-of select="_blank" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="_self" />
  </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

What I want is the following:

if NewWindow = Yes
  target = _blank
else
  target = _self

The code above just prints out Yes when I call it using {@NewWindow} or nothing at all when using {$target}.

Any and all help is appreciated!

Upvotes: 0

Views: 57

Answers (1)

JLRishe
JLRishe

Reputation: 101680

The value inside the select attribute is an XPath expression, not a static value. As an XPath expression, _blank would select elements with the name _blank.

To use a static value, you can either use <xsl:text>, or nothing at all.

With <xsl:text>:

<xsl:variable name="target">
  <xsl:choose>
    <xsl:when test="@NewWindow = 'Yes'">
      <xsl:text>_blank</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>_self<xsl:text>
  </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

With just the values:

<xsl:variable name="target">
  <xsl:choose>
    <xsl:when test="@NewWindow = 'Yes'">_blank</xsl:when>
    <xsl:otherwise>_self</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

Upvotes: 1

Related Questions