Search code examples
xsltxslt-2.0

How to Identify specific group of employees in large set of employees and assign them Y/N flag


I am looking to match certain group of employees from input file to pass YES or NO flag. It's based on employee id

Below is my source XML and XSLT I am trying. In below source code for employees 12121 and 12123 I need to mark them as YES in Approver column else it should be NO. For this requirement I am trying to use compare function. However it's not giving me accruate result. Is there any other way ? any other function I can use of ?

<?xml version='1.0' encoding='UTF-8'?>
<Report_Data>
<Report_Entry>
    <Employee_ID>12121</Employee_ID>
    <Tax_State_Code>NY</Tax_State_Code>
</Report_Entry>
<Report_Entry>
    <Employee_ID>12122</Employee_ID>
    <Tax_State_Code>PA</Tax_State_Code>
</Report_Entry>
<Report_Entry>
    <Employee_ID>12123</Employee_ID>
    <Tax_State_Code>PA</Tax_State_Code>
</Report_Entry>
<Report_Entry>
    <Employee_ID>12124</Employee_ID>
    <Tax_State_Code>PA</Tax_State_Code>
</Report_Entry>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

<xsl:variable name="comma">
    <xsl:text>,</xsl:text>
</xsl:variable>
<xsl:variable name="nextline">
    <xsl:text>&#xA;</xsl:text>
</xsl:variable>

<xsl:output method="text" omit-xml-declaration="yes"/>

<xsl:template match="/Report_Data">

    <xsl:text>EmployeeID,Approver,TaxState</xsl:text>

    <xsl:value-of select="$nextline"/>
    <xsl:for-each select="Report_Entry">

        <xsl:variable name="listA">
            <xsl:text>"12121","12123"</xsl:text>
        </xsl:variable>

        <xsl:variable name="empid" select="Employee_ID"/>

        <xsl:value-of select="$empid"/>
        <xsl:value-of select="$comma"/>

        <xsl:choose>
            <xsl:when test="compare($empid, $listA)">
                <xsl:text>YES</xsl:text>
            </xsl:when>
            <xsl:otherwise>NO</xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="$comma"/>

        <xsl:value-of select="Tax_State_Code"/>
        <xsl:value-of select="$nextline"/>
    </xsl:for-each>
</xsl:template>

Expected Output

EmployeeID,Approver,TaxState
12121,YES,NY
12122,NO,PA
12123,YES,PA
12124,NO,PA

Actual output currently getting is

EmployeeID,Approver,TaxState
12121,YES,NY
12122,YES,PA
12123,YES,PA
12124,YES,PA

Solution

  • This stylesheet:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" omit-xml-declaration="yes"/>
        <xsl:template match="/Report_Data">
            <xsl:value-of 
                select="'EmployeeID,Approver,TaxState',
                        Report_Entry
                            /string-join(
                                (Employee_ID,
                                 if (Employee_ID=(12121, 12123)) 
                                    then 'YES' 
                                    else 'NO',
                                 Tax_State_Code),
                                ','
                            )"
                separator="&#xA;"/>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    EmployeeID,Approver,TaxState
    12121,YES,NY
    12122,NO,PA
    12123,YES,PA
    12124,NO,PA
    

    Do note: the compare function has a signature with two string arguments (and the 3-arity function with collation argument) and you are indeed comparing the Employee_ID elemnt string value with the $listA text node string value '"12121","12123"'. For a one to many comparison you need to use the = operator.