Shaidar Haran
Shaidar Haran

Reputation: 31

XSLT: How do I get the value of a variable by concatenating a string and another variable?

I am trying to get the value of a variable by concatenating a string and another variable. But the result is a string with the variable name, not the value. So the following code fails since its trying to evaluate a string against a number. Also where the value should be, there is only the name of the variable.

The aim is to make a scrset for images ranging from 300px to maximum 4200. But stopping the srcset before it reaches the maxWidth value. So if an image has a maxWidth of 2000, then the iteration would stop after outputting 1800.

This is the code I have so far:

<xsl:variable name="count" select="14"/>
<xsl:variable name="maxWidth" select="2200"/> <!-- this value will be dynamic depending on each image (taken from an attribute on the image) -->

<xsl:variable name="loopIndex1" select="300"/> 
<xsl:variable name="loopIndex2" select="600"/>
<xsl:variable name="loopIndex3" select="900"/>
<xsl:variable name="loopIndex4" select="1200"/>
<xsl:variable name="loopIndex5" select="1500"/>
<xsl:variable name="loopIndex6" select="1800"/>
<xsl:variable name="loopIndex7" select="2100"/>
<xsl:variable name="loopIndex8" select="2400"/>
<xsl:variable name="loopIndex9" select="2700"/>
<xsl:variable name="loopIndex10" select="3000"/>
<xsl:variable name="loopIndex11" select="3300"/>
<xsl:variable name="loopIndex12" select="3600"/>
<xsl:variable name="loopIndex13" select="3900"/>
<xsl:variable name="loopIndex14" select="4200"/>

<xsl:attribute name="srcset"> 
    <xsl:for-each select="1 to $count">
        <xsl:variable name="index" select="position()"/>

        <xsl:variable name="source">
            <xsl:value-of select="concat('loopIndex', $index)"/>
        </xsl:variable>

        <xsl:if test="$source &lt; $maxWidth">
            http://imagescalerserver.com/?url=http://test.com/1108932.jpg&amp;w=<xsl:value-of select="concat($source, ' ')" /> <xsl:value-of select="$source" />w,
        </xsl:if>

    </xsl:for-each>
</xsl:attribute>

If I remove the test just to get some output, the output would be:

srcset="
http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=loopIndex1 loopIndex1w,
http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=loopIndex2 loopIndex2w,
etc
"

The wanted result is:

srcset="
http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=300 300w,
http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=600 600w,
etc
"

I also need to not have the comma after the last item. Meaning if http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=600 600w, was the last output then the comma at the end would not be there, like this:

http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=600 600w

Ideally I would like to not have to make the loopIndex variables, but rather just increment the value by 300 for a total of 14 iterations, but since variables cant be changed this is the best I've managed. If there is a better way, I'd appreciate to hear about it.

Upvotes: 0

Views: 60

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167571

Declare a single variable <xsl:variable name="loopIndex" select="300, 600, 900, ..., 4200"/> (you need to spell out the ... in your code) and then you can set

<xsl:variable name="source" select="$loopIndex[current()]"/>

inside of the for-each.

Upvotes: 1

Related Questions