我需要得到字符串的最后一部分在XSLT

问题描述:

你好,我需要得到ID键,它是在href部分,像我需要得到字符串的最后一部分在XSLT

<a href="/offer/something-is-verz-incorrect-1bMo.html"> some text </a> 

,我需要获得使用XSL 1bMo变量。

我尝试类似的东西:

<xsl:variable name="job_id3" select="str:split(substring-before(@href,'.html'), '-')[last()]"/> 
<xsl:attribute name="id"><xsl:value-of select="concat($ADAPTER_ID, '/', $job_id3)"/></xsl:attribute> 

我的XML输出必须是这样的:

<job id="adapter/1bMo""> 
     <str name="source">some source</str> 
     <str name="title">some title</str> 
... 
... 
    </job> 

现在我的XML输出这个样子的,这是错误的:

<job id="adapter/something-is-verz-incorrect-1bMo"> 
     <str name="source">some source.sk</str> 
     <str name="title">some title</str> 
... 
... 
    </job> 

但它会报错:URI http://exslt.org/strings不标识外部Java类。我不知道如何解决它,任何想法如何从每个href获得1bm9?

我的问题的答案就在这里:

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

,并调用模板:

<xsl:attribute name="job_id"> 
    <xsl:value-of select="concat($ADAPTER_ID, '/')"/> 
    <xsl:call-template name="substring-after-last"> 
        <xsl:with-param name="input" select="$job_id2" /> 
        <xsl:with-param name="marker" select="'-'" /> 
      </xsl:call-template> 
    </xsl:attribute> 

你可以建立与这个XPath变量:

substring-before(substring(/a/@href,string-length(/a/@href) - 8),'.') 

但它假定在href总是与名“.html”结尾,而ID关键始终是4个字符,所以这可能不是任何使用在所有:)

+0

肯定的,但ID有时有5个或6个字符... – miroyeah

+0

然后这将不起作用:D – smj

如果使用XSLT 2.0和假设ID将始终遵循的最后冲刺,你可以使用tokenize

<xsl:value-of select="tokenize(substring-before(/a/@href,'.html'),'-')[last()]"/> 

输入

<a href="/offer/something-is-verz-incorrect-1bMo.html"> some text </a> 

输出

1bMo 
+0

是...在xslt 2.0 :)但它是1.0 ...我做到了:) – miroyeah