从xml元素数据中删除最后一个字符

use*_*596 2 xml xslt xslt-1.0

我有一个xml节点如下

<root>
    <element>ABC,EFG, XYZ,<element>
</root>
Run Code Online (Sandbox Code Playgroud)

我想删除最后一个','.结果应该是ABC,EFG,XYZ我想使用XSL 1.0那种限制.

XSL我正在尝试使用

 <xsl:template match="/">
    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />

    <xsl:for-each select="//element">
        <xsl:if test="contains(substring(., string-length(.) - 1),$smallcase)">
            <xsl:value-of select="substring(., 1, string-length(.) - 1)"/>
        </xsl:if>
        <xsl:value-of select="substring(., string-length(.) - 1)"/>
    </xsl:for-each>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

Ian*_*rts 6

你可以用组合做到这一点substringstring-length:

substring(., 1, string-length(.) - 1)
Run Code Online (Sandbox Code Playgroud)

我不确定你当前的XSLT尝试做什么 - 它只会打印每个element元素的最后两个字符- 但尝试这样的事情:

<xsl:template match="/">
  <xsl:apply-templates select="//element"/>
</xsl:template>

<!-- match elements whose content ends with a comma, and strip it off -->
<xsl:template match="element[substring(., string-length()) = ',']">
  <xsl:value-of select="substring(., 1, string-length(.) - 1)" />
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

其他element元素(不以逗号结尾的元素)将由默认模板规则处理,该规则将完全打印出所有文本内容.