XSLT 2.0:for-each循环上的子串之后获取不同值

Aar*_*ron 2 xml xslt

我有以下输入XML:

<img src="blah.jpg"/>
<img src="blahh.jpg"/>
<img src="blah.gif"/>
Run Code Online (Sandbox Code Playgroud)

我需要一个不同的文件类型输出字符串,格式为:

jpg|gif
Run Code Online (Sandbox Code Playgroud)

我现有的样式表有一些方法:

<xsl:for-each select="distinct-values(descendant::img/@src)">
    <xsl:choose>
        <xsl:when test="position()!=last()">
            <xsl:value-of select="concat(substring-after(.,'.'),'|')"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring-after(.,'.')"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)

但是,我得到了一个重复的文件类型,因为我无法将for-for循环中的substring-after放入(它会引发错误,因为您无法一次对多个字符串执行substring-after)。这意味着我只能获取整个@src属性的不同值(而不是句点之后的字符串)。所以我的输出当前看起来像这样:

jpg|jpg|gif
Run Code Online (Sandbox Code Playgroud)

我非常感谢一个简单的XSLT 2.0解决方案,它使我能够做到这一点。

非常感谢您提前抽出宝贵时间-非常感谢。

Tim*_*m C 5

在XSLT 2.0中,您应该能够执行此操作。

<xsl:for-each select="distinct-values(descendant::img/(substring-after(@src, '.')))">
Run Code Online (Sandbox Code Playgroud)

那应该只返回“ jpg”和“ gif”。

更好的是,完全取消使用xsl:for-each,只需执行此操作

<xsl:value-of select="distinct-values(descendant::img/(substring-after(@src, '.')))" 
              separator="|" />
Run Code Online (Sandbox Code Playgroud)

当然,这假设所有文件名都只有一个.。文件名如“ my.file.gif”会引起问题。