我需要得到字符串的最后一部分在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个字符,所以这可能不是任何使用在所有:)
肯定的,但ID有时有5个或6个字符... – miroyeah
然后这将不起作用:D – smj