xslt中最后一个字符后的子字符串

xslt中最后一个字符后的子字符串,xslt,Xslt,我找不到这个问题的确切答案,所以我希望有人能在这里帮助我 我有一个字符串,我想得到最后一个“.”后面的子字符串。我正在使用XSLT1.0 这是怎么做到的?这是我的密码 <xsl:choose> <xsl:otherwise> <xsl:attribute name="class">method txt-align-left case-names</xsl:attribute>&#160; <x

我找不到这个问题的确切答案,所以我希望有人能在这里帮助我

我有一个字符串,我想得到最后一个“.”后面的子字符串。我正在使用XSLT1.0

这是怎么做到的?这是我的密码

<xsl:choose>
    <xsl:otherwise> 
        <xsl:attribute name="class">method txt-align-left case-names</xsl:attribute>&#160;
        <xsl:value-of select="./@name"/> // this prints a string eg: 'something1.something2.something3'
    </xsl:otherwise>
</xsl:choose>

方法txt左对齐大小写名称 ;
//这会打印一个字符串,例如:“something 1.something 2.something 3”

粘贴建议的代码时,会收到一条错误消息。“解析XSLT样式表失败。”

我想不出在XSLT 1.0中用一个表达式来实现这一点的方法,但可以用递归模板来实现:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <n>
      <xsl:call-template name="GetLastSegment">
        <xsl:with-param name="value" select="'something1.something2.something3'" />
        <xsl:with-param name="separator" select="'.'" />
      </xsl:call-template>
    </n>
  </xsl:template>

  <xsl:template name="GetLastSegment">
    <xsl:param name="value" />
    <xsl:param name="separator" select="'.'" />

    <xsl:choose>
      <xsl:when test="contains($value, $separator)">
        <xsl:call-template name="GetLastSegment">
          <xsl:with-param name="value" select="substring-after($value, $separator)" />
          <xsl:with-param name="separator" select="$separator" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$value" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

结果:

<n>something3</n>
something 3
我解决了它

<xsl:call-template name="GetLastSegment">
<xsl:with-param name="value" select="./@name" />
</xsl:call-template>

不需要

<xsl:with-param name="separator" value="'.'" />


在模板调用中,我使用xsl:function执行了相同的行为-使用会稍微简单一些:

<xsl:function name="ns:substring-after-last" as="xs:string" xmlns:ns="yourNamespace">
    <xsl:param name="value" as="xs:string?"/>
    <xsl:param name="separator" as="xs:string"/>        
    <xsl:choose>
        <xsl:when test="contains($value, $separator)">
            <xsl:value-of select="ns:substring-after-last(substring-after($value, $separator), $separator)" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$value" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:function>

您可以直接调用它,其值为:

<xsl:value-of select="ns:substring-after-last(.,'=')" xmlns:ns="yourNamespace"/>  

以下是一个解决方案:



(这里的
if
是因为如果字符串以分隔符结尾,tokenize将不会返回空字符串)

您可以这样做:
如果您无法实现此功能,是否可以编辑您的问题以包括XSLT的所有相关部分?命名空间dp来自哪里
dp:last之后的substring(substring之后的($value,$separator),$separator)”
谢谢Hayra。命名空间前缀中确实存在复制/粘贴错误。我刚刚更新了代码。哪个命名空间是ns?ns是您的命名空间。请参阅包含xmln:ns=“yourcompany.com”声明的第一行
<xsl:if test="substring($string, string-length($string)) != '.'"><xsl:value-of select="str:tokenize($string, '.')[last()]" /></xsl:if>