我有以下XSLT宏(在Umbraco中)
<xsl:param name="currentPage"/>
<xsl:template match="/">
<xsl:apply-templates select="$currentPage/imageList/multi-url-picker" />
</xsl:template>
<xsl:template match="url-picker">
<xsl:variable name="url"><xsl:value-of select="./url" /></xsl:variable>
<xsl:value-of select="concat('"', $url, '",')" />
</xsl:template>我不想将逗号添加到集合中的最后一个url选择器中。我该怎么做呢?
XML:模式,仅供参考:
<multi-url-picker>
<url-picker mode="URL">
<new-window>True</new-window>
<node-id />
<url>http://our.umbraco.org</url>
<link-title />
</url-picker>
<url-picker mode="Content">
<new-window>False</new-window>
<node-id>1047</node-id>
<url>/homeorawaytest2.aspx</url>
<link-title />
</url-picker>
<url-picker mode="Media">
<new-window>False</new-window>
<node-id>1082</node-id>
<url>/media/179/bolero.mid</url>
<link-title>Listen to this!</link-title>
</url-picker>
<url-picker mode="Upload">
<new-window>False</new-window>
<node-id />
<url>/media/273/slide_temp.jpg</url>
<link-title />
</url-picker>
发布于 2011-07-06 17:20:36
使用
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="url">
<xsl:if test="not(position()=1)">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="concat('"', ., '"')" />
</xsl:template>
</xsl:stylesheet>应用于此XML文档(未提供任何!):
<url-picker>
<url>1</url>
<url>2</url>
<url>3</url>
</url-picker>想要的,正确的结果产生
"1","2","3"确实注意到
不需要变量$url.
xsl:variable的select属性
而不是
<xsl:variable name="url"><xsl:value-of select="./url" /></xsl:variable>写
<xsl:variable name="url" select="url" />.3。对变量使用一些命名约定是一种很好的做法,这样如果不小心跳过了$,名称就不会很容易地与现有元素的名称相同。例如,使用:
<xsl:variable name="vUrl" select="url" />发布于 2011-07-06 16:34:55
我不确定它在您的情况下是否有效,因为我不确定调用url-picker模板的当前上下文,但是您可以添加一个xsl:if.
<xsl:template match="url-picker">
<xsl:variable name="url"><xsl:value-of select="./url" /></xsl:variable>
<xsl:value-of select="concat('"', $url, '"')" />
<xsl:if test="not(position()=last())">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:template>发布于 2011-07-06 16:38:19
您还可以检查下一个同级:
<xsl:template match="url-picker">
<xsl:variable name="url"><xsl:value-of select="./url" /></xsl:variable>
<xsl:value-of select="concat('"', $url, '"')" />
<xsl:if test="following-sibling::url-picker">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:template>https://stackoverflow.com/questions/6599705
复制相似问题