xsltxslt-3.0xslt-grouping

XSLT Variable assigning for each incremental delimiter position from the input XML


For the Input XML:

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

I want to have the values assigned to the variables like below in my XSLT. Delimiter is + and it should increment while assiging the values to each department variables in the XSLT so that I can use them get the below expected output ..

<xsl:value-of select="$departments[1]" /> should return the string "" 
<xsl:value-of select="$departments[2]" /> should return the string "Local_Functions" 
<xsl:value-of select="$departments[3]" /> should return the string "Local_Functions/Group_Functions" 
<xsl:value-of select="$departments[4]" /> should return the string "Local_Functions/Group_Functions/Group_Owners" 
<xsl:value-of select="$departments[5]" /> should return the string "Local_Functions/Group_Functions/Group_Owners/Group_Managers" 
<xsl:value-of select="$departments[6]" /> should return the string "Local_Functions/Group_Functions/Group_Owners/Group_Managers/Group_Leads" 
<xsl:value-of select="$departments[7]" /> should return the string "Local_Functions/Group_Functions/Group_Owners/Group_Managers/Group_Leads/Interim_Services" 
<xsl:value-of select="$departments[8]" /> should return the string "" 
<xsl:value-of select="$departments[9]" /> should return the string "" 
<xsl:value-of select="$departments[10]" /> should return the string "" 

I tried with

<xsl:variable name="departments" select="tokenize(/EmpJob/EmpJob/ConcatDepartment, '\+')"/>
<xsl:value-of select="$departments[1]" />

is returning the string ""

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

is returning the string "Local_Functions"

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

is returning the string "Group_Functions"

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

is returning the string "Group_Owners" and so on...Any help woth the XSLT will be really helpful


Solution

  • To get the behavior you want, use an expression like:

    <xsl:value-of select="$departments[position() le 4]" separator="/"/>
    

    to return the string:

    /Local_Functions/Group_Functions/Group_Owners
    

    Note that the expression:

    <xsl:value-of select="$departments[position() le 10]" separator="/"/>
    

    will return:

    /Local_Functions/Group_Functions/Group_Owners/Group_Managers/Group_Leads/Interim_Services
    

    and not an empty string. I am not sure what is the logic that should be used to produce the exact results you show.


    Added:

    To get a result like the one shown in your comment, do something like:

    <xsl:stylesheet version="3.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8" />
    
    <xsl:template match="/EmpJob">
        <xsl:variable name="departments" select="tokenize(EmpJob/ConcatDepartment, '\+')"/>
        <xsl:for-each select="1 to count($departments)">
            <xsl:text>Line</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>,"</xsl:text>
            <xsl:value-of select="$departments[position() le current()]" separator="/"/>
            <xsl:text>"&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
        
    </xsl:stylesheet>