2013-05-07 116 views
0

我正在使用XSLT 2.0並嘗試提取所需的caracterer正則表達式。正則表達式XSLT,提取匹配

我試着用tokenize(),但更令人費解。

<xsl:value-of select="tokenize('2.0-Cloverleaf-3d', '-')" /> 

通過其結果是:2.0,應當返回的結果是:。字符串是(2.0-Cloverleaf- * * d)。

我更願意做這樣:

<xsl:if test="matches(x:td/x:p, '.+3d$')"> 
    true 
    </xsl:if> 

有什麼功能得到你需要的字符,而不需要一個「如果」?謝謝。

編輯

我想我會離開它這樣。

<xsl:variable name="places"> 
    <xsl:if test="matches(x:td/x:p, '.+3d$')">3</xsl:if> 
    <xsl:if test="matches(x:td/x:p, '.+4d$')">4</xsl:if> 
    <xsl:if test="matches(x:td/x:p, '.+5d$')">5</xsl:if> 
</xsl:variable> 

但是,如果有更好的方法來做到這一點。謝謝。

+1

你想返回或得到什麼樣的字符?我想你只是想檢查字符串是否以'3d'結尾,因爲我們在2.0中有一個函數是ends-with(),但它返回布爾值。如果你有固定的長度,你也可以使用substring()。 – 2013-05-07 15:29:14

+0

我喜歡這樣,結束與()。謝謝。 – 2013-05-07 15:31:10

回答

3

您可以使用替換,用您需要的部分替換所有部分。

在XPath 2:

replace(x:td/x:p, '^.*([0-9])d$', '$1') 

在XSL:

<xsl:value-of select="replace(x:td/x:p, '^.*([0-9])d$', '$1')" /> 
+0

正是我所需要的。謝謝 :-)。 – 2013-05-07 16:47:55

0

你可以做到這一點與tokenize()但首先收集令牌到一個變量。

<xsl:variable name="tokens" select="tokenize(x:td/x:p,'-')"/> 

然後選擇最後一個標記並使用substring()獲取數字。

<xsl:value-of select="substring(item-at($tokens, 3), 1, 1)" />