为什么 substring-before() 在 XSLT 中对我不起作用?

Why is substring-before() not working for me in XSLT?

我试图使用一些字符串函数在 XSLT 中提取文件名的一部分,但不明白为什么它不起作用。

这是我的 XSLT 示例 运行:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:str="http://exslt.org/strings">
  <xsl:output method="text"/>
  <xsl:variable name="A" >some.filename.test.CCYYMMDD.xml</xsl:variable>
  <xsl:variable name="fileDateSplitChar" select="'.'"/>
  <xsl:template match="/">
    <xsl:text>split char : </xsl:text>
    <xsl:value-of select="str:split($A,$fileDateSplitChar)[last()]"/>
    <xsl:text>split nodeset : </xsl:text>
    <xsl:value-of select="str:split($A,$fileDateSplitChar)"/>
    <xsl:text>substring-before : </xsl:text>
    <xsl:value-of select="substring-before(str:split($A,$fileDateSplitChar)[last()],
                                                                           'xml')"/>
  </xsl:template>
</xsl:stylesheet> 

我试图实现的是提取文件名的一部分,CCYYMMDD。当我 运行 它时,第一条消息正确显示拆分字符;第二条消息 returns 值 'some';但是第三条消息的结果是空白。

我正在使用 XSLT 1.0。

EXSLT str:split 函数 returns 节点集(因为这是 XSLT 1.0 中唯一可用的数据结构)。 <xsl:value-of>,应用于节点集时,显示第一个节点的字符串值,并忽略所有其他节点。类似地,substring-before() 在应用于节点集时处理集合中的第一个节点,并忽略其余部分。

您真的无法升级到 XSLT 2.0 吗?

but the result of the third message is blank.

结果为空,因为:

str:split($A,$fileDateSplitChar)[last()]

returns"xml",以及:

substring-before('xml, 'xml')" 

returns 一个空字符串。

What I was trying to achieve is to extract the part of the file CCYYMMDD.

我假设您不知道这部分的顺序位置,只知道它是最后一个 - 因此可以使用以下方式提取它:

str:split($A,$fileDateSplitChar)[last() - 1]