给定一个表格编号,例如:
ABC_12345_Q-10
我想结束:
ABC12345
所以我需要找到第二个下划线的位置
请注意,下划线之间的任何“部分”都没有标准模式或长度(因此我不能使用子字符串来简单地消除最后一部分)。
xPath 2.0 解决方案还可以。
@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
XSLT 2.0 中更简单的解决方案:
codepoints-to-string(reverse(string-to-codepoints(
substring-before(
codepoints-to-string(reverse(string-to-codepoints($s))), '_'))))
使用“substring-before”,您将获得最后一次出现分隔符(下划线)之后的所有内容。如果您改用“substring-after”,您将在最后一次出现分隔符之前获得所有内容。
concat(
substring-before($s, '_'),
substring-before(substring-after($s, '_'), '_')
)
或者:
string-join(tokenize($s, '_')[position() <= 2], '')
广义一——
substring($string,1, string-length($string)-string-length(str:split($string, '_')[count(str:split($string, '_'))]))
想法是通过拆分字符串来获取最后一次出现的索引。
最后出现的索引 = string-length($string) - 拆分后最后一个字符串的长度