【问题标题】:Substring after last character in xsltxslt 中最后一个字符后的子字符串
【发布时间】:2013-07-04 11:16:14
【问题描述】:

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

我有一个字符串,我想得到最后一个'.'之后的子字符串。我正在使用 xslt 1.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>

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

【问题讨论】:

    标签: xslt


    【解决方案1】:

    我想不出用 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>
    

    【讨论】:

    • 你会这样做:&lt;xsl:call-template name="GetLastSegment"&gt;&lt;xsl:with-param name="value" select="@name" /&gt;&lt;xsl:with-param name="separator" value="'.'" /&gt;&lt;/xsl:call-template&gt; 如果你不能让它工作,你能编辑你的问题以包括你的 XSLT 的所有相关部分吗?
    • 如何获得位置值?喜欢什么?
    【解决方案2】:

    我对 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>
    

    你可以直接在 value-of 中调用它:

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

    【讨论】:

    • 命名空间 dp 来自哪里? dp:substring-after-last(substring-after($value, $separator), $separator)"
    • 谢谢海拉。命名空间前缀中确实存在复制/粘贴错误。我刚刚更新了代码。
    • ns 是哪个命名空间?
    • ns 是您的命名空间。请参阅包含声明的第一行 xmln:ns="yourcompany.com"
    【解决方案3】:

    这是使用EXSLT str:tokenize的解决方案:

    <xsl:if test="substring($string, string-length($string)) != '.'"><xsl:value-of select="str:tokenize($string, '.')[last()]" /></xsl:if> 
    

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

    【讨论】:

      【解决方案4】:

      我解决了

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

      不需要

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

      在模板调用中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-19
        • 2019-05-13
        • 2013-07-29
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多