Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4886

How to apply if else statement on XSLT variable

I have a xSLT file like this

<?xml version="1.0" encoding="UTF-8" ?>

<!-- New document created with EditiX at Thu Feb 18 19:08:23 IST 2016 -->

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
    xmlns:err="http://www.w3.org/2005/xqt-errors"
    exclude-result-prefixes="xs xdt err fn">

    <xsl:output method="xml" indent="yes"/>

    <xsl:variable name="DIVISION_CODE">
    47
    </xsl:variable>
    <xsl:variable name="DEPT_CODE">
    KT
    </xsl:variable>
    <xsl:variable name="JOB_LEVEL">
    4B
    </xsl:variable>

    <xsl:variable name="MI_GROUP">
    </xsl:variable>

    <xsl:variable name="A_Div_Code">
    47,48,49,50
    </xsl:variable>
    <xsl:variable name="A_Dept_Code">
    KT,VT,SF,MQ
    </xsl:variable>
    <xsl:variable name="A_Job_Level">
    4B,2B,7B
    </xsl:variable>

    <xsl:variable name="D_Div_Code">
    70,65,42,31
    </xsl:variable>
    <xsl:variable name="D_Dept_Code">
    LM,MX,PQ
    </xsl:variable>
    <xsl:variable name="D_Job_Level">
    4D,2D,3D
    </xsl:variable> 

    <xsl:variable name="C_Div_Code">
    12,76,31
    </xsl:variable>
    <xsl:variable name="C_Dept_Code">
    ML,KL,KO
    </xsl:variable>
    <xsl:variable name="C_Job_Level">
    4C,2C,7C
    </xsl:variable>  
</xsl:stylesheet>

So basically I have 3 variables which will have data from server

<xsl:variable name="DIVISION_CODE">
    47
</xsl:variable>
<xsl:variable name="DEPT_CODE">
    KT
</xsl:variable>
<xsl:variable name="JOB_LEVEL">
    4B
</xsl:variable>

I have to check these variables against the other predefined variables.

My Sudo Code is as follow

If ( A_Div_Code contain '$DIVISION_CODE') and  ( A_Dept_Code contains '$DEPT_CODE') and (A_job_Level contains '$JOB_LEVEL)
MP_GROUP='A'

I learnt on W3chools that I will have to use

<xsl:choose></xsl:choose>

But I don't know how to target the variable

Can any one give me an example?

UPDATED XSLT FILES

<?xml version="1.0" encoding="UTF-8" ?>

<!-- New document created with EditiX at Thu Feb 18 19:08:23 IST 2016 -->

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
    xmlns:err="http://www.w3.org/2005/xqt-errors"
    exclude-result-prefixes="xs xdt err fn">

    <xsl:output method="xml" indent="yes"/>

    <xsl:variable name="DIVISION_CODE" select="'47'" /> 
    <xsl:variable name="DEPT_CODE" select="'KT'" /> 
    <xsl:variable name="JOB_LEVEL" select="4B''" /> 




<xsl:variable name="A_Div_Code" select="'47,48,49,50
'" /> 

<xsl:variable name="A_Dept_Code" select="'KT,VT,SF,MQ

'" /> 


<xsl:variable name="A_Job_Level" select="'4B,2B,7B
'" /> 



<xsl:variable name="D_Div_Code" select="'70,65,42,12,76,31,47,48,49,50

'" /> 

<xsl:variable name="D_Dept_Code" select="'LM,MX,PQ,ML,KL,KO
,KT,VT,SF,MQ


'" /> 


<xsl:variable name="D_Job_Level" select="'4D,2D,3D

,4C,2C,7C,4B,2B,7B'" />





<xsl:variable name="C_Div_Code" select="'12,76,31,47,48,49,50

'" /> 

<xsl:variable name="C_Dept_Code" select="'ML,KL,KO
,KT,VT,SF,MQ

'" /> 


<xsl:variable name="C_Job_Level" select="'4C,2C,7C,4B,2B,7B
'" />




<xsl:choose>
        <xsl:when test="contains($A_Div_Code, $DIVISION_CODE) and contains($A_Dept_Code, $DEPT_CODE) and contains($A_job_level, $JOB_LEVEL)">


<xsl:variable name="MI_GROUP" select="'A
'" /> 
        </xsl:when>
    <xsl:when test="contains($C_Div_Code, $DIVISION_CODE) and contains($C_Dept_Code, $DEPT_CODE) and contains($C_job_level, $JOB_LEVEL)">


<xsl:variable name="MI_GROUP" select="'A
,C'" /> 
        </xsl:when>
       <xsl:when test="contains($D_Div_Code, $DIVISION_CODE) and contains($D_Dept_Code, $DEPT_CODE) and contains($D_job_level, $JOB_LEVEL)">


<xsl:variable name="MI_GROUP" select="'A
,C,D'" /> 
        </xsl:when>
         <xsl:otherwise>
            <xsl:variable name="MI_GROUP" select="'A
,C,D,Z'" /> 
        </xsl:otherwise>
    </xsl:choose>

</xsl:stylesheet>

Error

ERROR - Looks like XML you provided is not valid: Error on line -1 of document : Premature end of file. Nested exception: Premature end of file.

Thanks

Upvotes: 1

Views: 4054

Answers (1)

Dan Field
Dan Field

Reputation: 21641

If you just have a simple one condition if, use xsl:if. If you need to handle "else if" and "else", use xsl:choose. For example:

<xsl:if test="contains($A_Div_Code, $DIVISION_CODE) and contains($A_Dept_Code, $DEPT_CODE) and contains($A_job_level, $JOB_LEVEL)>
    <!-- output what you want to here -->
</xsl:if>

The same written as a choose would be:

<xsl:choose>
    <xsl:when test="contains($A_Div_Code, $DIVISION_CODE) and contains($A_Dept_Code, $DEPT_CODE) and contains($A_job_level, $JOB_LEVEL)>
        <!-- output what you want to here -->
    </xsl:when>
    <xsl:when test="some other logical test...">
        <!-- output what you want to here - but note that this won't get executed if the first test succeeded! -->
    </xsl:when>
    <xsl:otherwise>
        <!-- output default here -->
    </xsl:otherwise>
</xsl:choose>

Note that only the first when will match, and others after will be ignroed even if they would have matched.

One last point - you probably don't want to use this to assign variables. Those variables can only be used in the scope of the <xsl:if>, and if they're declared before that scope they can't be assigned to later (variables are readonly in XSLT, they can't be assigned to after declaration like in many other languages):

<xsl:if test="true()">
    <xsl:variable name="testing" select="'asdf'"/>
</xsl:if>

<xsl:value-of select="$testing" /> <!-- error! $testing isn't in scope-->

and

<xsl:variable name="testing" select="'foo'" />
<xsl:if test="true()">
    <xsl:variable name="testing" select="'asdf'"/>
</xsl:if>

<xsl:value-of select="$testing" /> <!-- output will be 'foo', not 'asdf'! -->

Because of this, it's generally best to use templates to match nodesets that meet certain conditions rather than to use if and choose statements:

<xsl:template match="/path/to/node[DIVISION_CODE = $A_Div_Code] and ..." >

This template will then give you the ability to do what you need to, and will generally be easier to manage and be more portable than if it were embedded in a choose statement.

Upvotes: 2

Related Questions