Search code examples
xsltsharepoint-2010sharepoint-designerxslt-1.0

How can I use the value of a variable for the use property of a key in XSLT, I want to achieve use="$Variable" in the key tag;;


Suppose I have a key defined in an xslt file in SharePoint Designer 2010 as:

<xsl:key name="Years" match="/dsQueryResponse/Rows/Row" use="@Date" />

Where @Date is the column, however instead of @Date, I want to use the value of the following variable:

<xsl:variable name="VarNAme">
        <xsl:choose>
           <xsl:when test="string-length(@Date) = 8">
                <xsl:value-of select="substring(@Date, 5, 4)"></xsl:value-of>
            </xsl:when>
            <xsl:when test="string-length(@Date) = 9">
                <xsl:value-of select="substring(@Date, 6, 4)"></xsl:value-of>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring(@Date, 7, 4)"></xsl:value-of>
            </xsl:otherwise>
        </xsl:choose>   
</xsl:variable>

If there is a better way (one-liner) to get just the year from a date, I'd welcome that as well. I want to use generate-id to get distinct years (not dates, years).


Solution

  • <xsl:key 
      name="Years" 
      match="/dsQueryResponse/Rows/Row" 
      use="substring(@Date, string-length(@Date) - 3, 4)"
    />
    

    Hint

     8 - 3 = 5
     9 - 3 = 6
    10 - 3 = 7
    

    ;-)