0

I am trying to compare a xml value with set of xslt variables but the output is empty

Here is my xslt file

<?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">
    <xsl:output method="xml" indent="yes"/>
        <xsl:variable name="MI_GROUP"/>
    <xsl:template match="/">


        <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:for-each 
        select="COMPANY/EMPLOYEE" >

        <xsl:variable name="DIVISION_CODE" select="'DIVISION_CODE'"/>
        <xsl:variable name="DEPT_CODE" select="'DEPT_CODE'"/>
        <xsl:variable name="JOB_LEVEL" select="'JOB_LEVEL'"/>

        <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>
        <tr>
            <th><xsl:value-of select="NAME"/></th><th><xsl:value-of select="$MI_GROUP"/></th>
        </tr>
    </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>

And the employeedetails.xml is as follows

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="employee-detailcheck.xsl"?>

<COMPANY>

<EMPLOYEE>
    <DIVISION_CODE>47</DIVISION_CODE>
    <DEPT_CODE>KT</DEPT_CODE>
    <JOB_LEVEL>4B</JOB_LEVEL>
    <NAME>Vikram</NAME>
</EMPLOYEE>
<EMPLOYEE>
    <DIVISION_CODE>70</DIVISION_CODE>
    <DEPT_CODE>LM</DEPT_CODE>
    <JOB_LEVEL>4D</JOB_LEVEL>
    <NAME>VISHWAS</NAME>
</EMPLOYEE>
<EMPLOYEE>
    <DIVISION_CODE>47</DIVISION_CODE>
    <DEPT_CODE>KT</DEPT_CODE>
    <JOB_LEVEL>4B</JOB_LEVEL>
    <NAME>Radzie</NAME>
</EMPLOYEE>
<EMPLOYEE>
    <DIVISION_CODE>12</DIVISION_CODE>
    <DEPT_CODE>ML</DEPT_CODE>
    <JOB_LEVEL>4C</JOB_LEVEL>
    <NAME>Vishnu</NAME>
</EMPLOYEE>

</COMPANY>

Can any one let me know why the $MI_GROUP variable is showing empty .

Thanks

Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130

2 Answers2

1

Because it uses the variable $MI_GROUP defined after xsl:output and not one of the variables defined inside of the <xsl:choose>.

To get your output you need to define

<xsl:variable name="MI_GROUP">
<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:value-of select="'A'" />
    </xsl:when>
    ... other cases...
    <xsl:otherwise>
        <xsl:value-of select="'A,C,D,Z'" />
    </xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr>
    <th><xsl:value-of select="NAME"/></th><th><xsl:value-of select="$MI_GROUP"/></th>
</tr>
wero
  • 32,544
  • 3
  • 59
  • 84
1

Two problems can be found here:

  1. You are trying to display the value of the variable but it is out of the scope where this variable has been declared.

  2. you are improperly trying to retrieve text content. Instead of <xsl:variable name="DIVISION_CODE" select="'DIVISION_CODE'"/>, you need to used something like : <xsl:variable name="DIVISION_CODE" select="DIVISION_CODE/text()"/>. In your initial code you are only setting your variable to the DIVISION_CODE string.

Refactoring that way should work:

<?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">
    <xsl:output method="xml" indent="yes"/>
        <xsl:variable name="MI_GROUP"/>
        <xsl:template match="/">


        <xsl:variable name="DIVISION_CODE" select="DIVISION_CODE/text()"/>
        <xsl:variable name="DEPT_CODE" select="DEPT_CODE/text()"/>
        <xsl:variable name="JOB_LEVEL" select="JOB_LEVEL/text()"/>

        <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:for-each 
        select="COMPANY/EMPLOYEE" >

        <xsl:variable name="DIVISION_CODE" select="'DIVISION_CODE'"/>
        <xsl:variable name="DEPT_CODE" select="'DEPT_CODE'"/>
        <xsl:variable name="JOB_LEVEL" select="'JOB_LEVEL'"/>

        <xsl:variable name="MI_GROUP">
        <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:text>A</xsl:text>
            </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:text>A,C</xsl:text>
            </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:text>A,C,D</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>A,C,D,Z</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
        </xsl:variable>
        <tr>
            <th><xsl:value-of select="NAME"/></th><th><xsl:value-of select="$MI_GROUP"/></th>
        </tr>
    </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>
potame
  • 7,597
  • 4
  • 26
  • 33
  • Yes I am getting some value but Its still not matching properly its showing me the value of ` A,C,D,Z ` this part of the code , Can you help me format the content of the XSL variable so that I can match the string properly – Vikram Anand Bhushan Feb 23 '16 at 13:50
  • Then please explain further the logic of what you would like to do and provide a sample output of what you expect to obtain. Thank you. – potame Feb 23 '16 at 13:52
  • okay , I am getting the employee detail from the XML , I am trying to match it with the variable defined inside the XSLT , For example , if you take ` 70 LM 4D VISHWAS ` this par of the XML , It should display A,C,D as the out put – Vikram Anand Bhushan Feb 23 '16 at 13:59
  • 1
    OK, I get it. I updated the answer. you can also go to http://xsltransform.net/3NJ38ZL too see it work "live". – potame Feb 23 '16 at 14:23