XSLT:如何通过连接字符串和另一个变量来获取变量的值?
XSLT: How do I get the value of a variable by concatenating a string and another variable?
我试图通过连接一个字符串和另一个变量来获取变量的值。但结果是一个带有变量名的字符串,而不是值。所以下面的代码失败了,因为它试图根据数字评估一个字符串。还有值应该在的地方,只有变量的名字。
目的是为从 300 像素到最大 4200 像素的图像制作一个 srcset。但是在它达到 maxWidth 值之前停止 srcset。因此,如果图像的最大宽度为 2000,则迭代将在输出 1800 后停止。
这是我目前的代码:
<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 < $maxWidth">
http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=<xsl:value-of select="concat($source, ' ')" /> <xsl:value-of select="$source" />w,
</xsl:if>
</xsl:for-each>
</xsl:attribute>
如果我删除测试只是为了获得一些输出,输出将是:
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
"
想要的结果是:
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
"
我还需要在最后一项后面不要有逗号。意思是如果 http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=600 600w,
是最后一个输出那么末尾的逗号就不会存在,像这样:
http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=600 600w
理想情况下,我不想创建 loopIndex 变量,而只是将值增加 300 以进行总共 14 次迭代,但由于无法更改变量,这是我管理过的最好的方法。如果有更好的方法,我将不胜感激。
声明单个变量<xsl:variable name="loopIndex" select="300, 600, 900, ..., 4200"/>
(需要在代码中拼出...
)然后就可以设置
<xsl:variable name="source" select="$loopIndex[current()]"/>
在 for-each
.
里面
我试图通过连接一个字符串和另一个变量来获取变量的值。但结果是一个带有变量名的字符串,而不是值。所以下面的代码失败了,因为它试图根据数字评估一个字符串。还有值应该在的地方,只有变量的名字。
目的是为从 300 像素到最大 4200 像素的图像制作一个 srcset。但是在它达到 maxWidth 值之前停止 srcset。因此,如果图像的最大宽度为 2000,则迭代将在输出 1800 后停止。
这是我目前的代码:
<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 < $maxWidth">
http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=<xsl:value-of select="concat($source, ' ')" /> <xsl:value-of select="$source" />w,
</xsl:if>
</xsl:for-each>
</xsl:attribute>
如果我删除测试只是为了获得一些输出,输出将是:
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
"
想要的结果是:
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
"
我还需要在最后一项后面不要有逗号。意思是如果 http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=600 600w,
是最后一个输出那么末尾的逗号就不会存在,像这样:
http://imagescalerserver.com/?url=http://test.com/1108932.jpg&w=600 600w
理想情况下,我不想创建 loopIndex 变量,而只是将值增加 300 以进行总共 14 次迭代,但由于无法更改变量,这是我管理过的最好的方法。如果有更好的方法,我将不胜感激。
声明单个变量<xsl:variable name="loopIndex" select="300, 600, 900, ..., 4200"/>
(需要在代码中拼出...
)然后就可以设置
<xsl:variable name="source" select="$loopIndex[current()]"/>
在 for-each
.