Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
1.7k views
in SharePoint Server by 30 37 42

In SharePoint 2016, I need to implement Nested IF-ELSE conditions in Itemstyle.xsl to be used in Content Query Web Part (CQWP), one of my requirements is to check if the current field value is equal to x, it should do something else if do another something! I need to avoid writing multiple IF conditions like this!

<xsl:if test="$Itemsvar = 'True'">
	<xsl:value-of select="@OnClickForWebRendering"/>
</xsl:if>
<xsl:if test="$Itemsvar!= 'True' and @OpenInNewWindow = 'True'">
	<xsl:value-of disable-output-escaping="yes" select="$OnClickTargetAttribute"/>
</xsl:if>

How I can implement Multiple IF-ELS conditions in XSLT ItemStyle.xsl file in SharePoint 2016? to be more specific I need to apply the SWITCH statement in XSLT, How I can do that?


1 Answer

1 like 0 dislike
by 159 200 385
selected by
 
Best answer

Implementing IF-ELSE condition in ItemStyle.xsl file in SharePoint

In XSLT, there is no IF-ELS condition. However, you can use choose statement to perform IF-ELSE and NESTED IF in XSLT as the following:

<xsl:choose>
    <xsl:when test="">
       <!--do something-->
   </xsl:when>
   <xsl:otherwise>
       <!-- Else do somthing -->
    </xsl:otherwise>
</xsl:choose>

Implementing NESTED-IF-ELSE in SharePoint ItemStyle.xsl file

If you would like to implement multiple IF in ItemStyle.xsl file, you have to repeat when clause as the following:

<xsl:choose>
    <xsl:when test="">
       <!--do something-->
   </xsl:when>
    <xsl:when test="">
       <!--do something-->
   </xsl:when>
    <xsl:when test="">
       <!--do something-->
   </xsl:when>
   <xsl:otherwise>
       <!-- Else do somthing -->
    </xsl:otherwise>
</xsl:choose>

Implementing NESTED CHOOSE in SharePoint ItemStyle.xsl file

Besides the above examples, you can also use NESTED CHOOSE in XSLT as the following:

<xsl:choose>
    <xsl:when test="">
         <xsl:choose>
           <xsl:when test="">
            <!--do something-->
            </xsl:when>
             <xsl:otherwise>
             <!-- Else do somthing -->
           </xsl:otherwise>
         </xsl:choose>
   </xsl:when>
    <xsl:when test="">
       <!--do something-->
   </xsl:when>
    <xsl:when test="">
       <!--do something-->
   </xsl:when>
   <xsl:otherwise>
       <!-- Else do somthing -->
    </xsl:otherwise>
</xsl:choose>

NESTED-IF and Multiple CHOOSE in XSLT

Below is an example to perform Nested-IF and Multiple CHOOSE in XSLT ItemStyle file in SharePoint

NESTED IF-ELSE in XSLT

If you don’t ask, the answer is always NO!
...