XSLT:查找字符串中的最后一次出现

问题描述 投票:8回答:4

给出如下表格编号:

ABC_12345_Q-10

我想最终得到:

ABC12345

所以我需要找到第二个下划线的位置

请注意,下划线之间的任何“部分”都没有标准模式或长度(因此我不能使用子字符串来简单地删除最后一部分)。

xPath 2.0解决方案没问题。

xslt xpath xslt-2.0
4个回答
2
投票
concat(
    substring-before($s, '_'),
    substring-before(substring-after($s, '_'), '_')
)

或者:

string-join(tokenize($s, '_')[position() <= 2], '')

11
投票

@Pavel_Minaev提供了XPath 1.0和XPath 2.0解决方案,如果预先知道下划线的数量为2,则该解决方案有效。

以下是更难解决的问题的解决方案,其中未分析的数量不是静态的(可以是任何数字):

XPath 2.0:

translate(substring($s,
                    1, 
                    index-of(string-to-codepoints($s), 
                             string-to-codepoints('_')
                             )[last()] -1
                   ),
          '_',
          ''
         )

XSLT 1.0:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 >
 <xsl:output method="text"/>

  <xsl:variable name="s" select="'ABC_12345_Q-10'"/>
  <xsl:template match="/">
    <xsl:call-template name="stripLast">
     <xsl:with-param name="pText" select="$s"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="stripLast">
    <xsl:param name="pText"/>
    <xsl:param name="pDelim" select="'_'"/>

     <xsl:if test="contains($pText, $pDelim)">
       <xsl:value-of select="substring-before($pText, $pDelim)"/>
       <xsl:call-template name="stripLast">
         <xsl:with-param name="pText" select=
          "substring-after($pText, $pDelim)"/>
         <xsl:with-param name="pDelim" select="$pDelim"/>
       </xsl:call-template>
     </xsl:if>
   </xsl:template>
</xsl:stylesheet>

当此转换应用于任何XML文档(未使用)时,将生成所需的正确结果:

ABC12345

5
投票

XSLT 2.0中更容易的解决方案:

codepoints-to-string(reverse(string-to-codepoints(
    substring-before(
        codepoints-to-string(reverse(string-to-codepoints($s))), '_'))))

使用'substring-before',您将在最后一次出现分隔符(下划线)后获得所有内容。如果您使用'substring-after'代替,您将在最后一次出现deliminator之前获得所有内容。


0
投票

广义的 -

substring($string,1, string-length($string)-string-length(str:split($string, '_')[count(str:split($string, '_'))]))

想法是通过拆分字符串来获取最后一次出现的索引。

最后一次出现的索引= string-length($ string) - 拆分后的最后一个字符串的长度

© www.soinside.com 2019 - 2024. All rights reserved.