如何在 XSLT 1.0 中获取具有唯一数字的数字?
How to get a number with unique digits in XSLT 1.0?
我有以下变量。
<xsl:variable name="number" select="56568"/>
需要输出:568
我需要得到一个只包含数字中唯一数字的输出。
知道如何在 XSLT 1.0 中实现这一点吗?
谢谢
我认为没有简单的方法可以做到这一点 - 除非您的处理器支持某些扩展功能。没有它,您将不得不使用递归命名模板:
<xsl:template name="distinct-characters">
<xsl:param name="input"/>
<xsl:param name="output"/>
<xsl:choose>
<xsl:when test="not($input)">
<xsl:value-of select="$output"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="char" select="substring($input, 1, 1)" />
<!-- recursive call -->
<xsl:call-template name="distinct-characters">
<xsl:with-param name="input" select="substring($input, 2)"/>
<xsl:with-param name="output">
<xsl:value-of select="$output"/>
<xsl:if test="not(contains($output, $char))">
<xsl:value-of select="$char"/>
</xsl:if>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
调用示例:
<output>
<xsl:call-template name="distinct-characters">
<xsl:with-param name="input" select="56568"/>
</xsl:call-template>
</output>
结果:
<output>568</output>
演示:http://xsltransform.net/a9Gix1
已添加:
时隔4年重温,有更短更高效的方法。它不是遍历输入中的每个字符,而是仅遍历每个 distinct 字符:
<xsl:template name="distinct-characters">
<xsl:param name="input"/>
<xsl:if test="$input">
<xsl:variable name="char" select="substring($input, 1, 1)" />
<xsl:value-of select="$char"/>
<!-- recursive call -->
<xsl:call-template name="distinct-characters">
<xsl:with-param name="input" select="translate($input, $char, '')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
我有以下变量。
<xsl:variable name="number" select="56568"/>
需要输出:568
我需要得到一个只包含数字中唯一数字的输出。
知道如何在 XSLT 1.0 中实现这一点吗?
谢谢
我认为没有简单的方法可以做到这一点 - 除非您的处理器支持某些扩展功能。没有它,您将不得不使用递归命名模板:
<xsl:template name="distinct-characters">
<xsl:param name="input"/>
<xsl:param name="output"/>
<xsl:choose>
<xsl:when test="not($input)">
<xsl:value-of select="$output"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="char" select="substring($input, 1, 1)" />
<!-- recursive call -->
<xsl:call-template name="distinct-characters">
<xsl:with-param name="input" select="substring($input, 2)"/>
<xsl:with-param name="output">
<xsl:value-of select="$output"/>
<xsl:if test="not(contains($output, $char))">
<xsl:value-of select="$char"/>
</xsl:if>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
调用示例:
<output>
<xsl:call-template name="distinct-characters">
<xsl:with-param name="input" select="56568"/>
</xsl:call-template>
</output>
结果:
<output>568</output>
演示:http://xsltransform.net/a9Gix1
已添加:
时隔4年重温,有更短更高效的方法。它不是遍历输入中的每个字符,而是仅遍历每个 distinct 字符:
<xsl:template name="distinct-characters">
<xsl:param name="input"/>
<xsl:if test="$input">
<xsl:variable name="char" select="substring($input, 1, 1)" />
<xsl:value-of select="$char"/>
<!-- recursive call -->
<xsl:call-template name="distinct-characters">
<xsl:with-param name="input" select="translate($input, $char, '')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>