xsltxslt-1.0xslt-2.0xslt-3.0xslt-grouping

XSLT Variable assigning for each delimiter position from the input XML


In my XSLT I need to assign the values to the variable named FODepartment_LX where X represents the position of the delimiter(+) starting from the leftmost delimiter and Maximum position is 10. If the position of the delimiter doesn't give any value, set the variable is to an empty string ('') for remaininag FODepartment_LX.

Input XML:

<EmpJob>
    <EmpJob>
        <ConcatDepartment>+Local_Functions+Group_Functions+Group_Owners+Group_Managers+Group_Leads+Interim_Services</ConcatDepartment>
    </EmpJob>
</EmpJob>

Expected Output in the XSLT so that I can use it for further processing:

<xsl:variable name="FODepartment_L10" select=""/>
<xsl:variable name="FODepartment_L9" select=""/>
<xsl:variable name="FODepartment_L8" select=""/>
<xsl:variable name="FODepartment_L7" select=""/>
<xsl:variable name="FODepartment_L6" select="Interim_Services"/>
<xsl:variable name="FODepartment_L5" select="Group_Leads"/>
<xsl:variable name="FODepartment_L4" select="Group_Managers"/>
<xsl:variable name="FODepartment_L2" select="Group_Owners"/>
<xsl:variable name="FODepartment_L2" select="Group_Functions"/>
<xsl:variable name="FODepartment_L1" select="Local_Functions"/>

How can I achieve this in the XSLT?


Solution

  • If you define a single variable as:

    <xsl:variable name="departments" select="tokenize(/EmpJob/EmpJob/ConcatDepartment, '\+')"/>
    

    you can then refer to the individual tokens, for example:

    <xsl:value-of select="$departments[2]" />
    

    will return the string "Local_Functions" and:

    <xsl:value-of select="$departments[10]" />
    

    will return an empty string.