2016-02-24 99 views
0

正則表達式應該驗證這樣的話A23-abcefghijk正則表達式錯誤

<xsl:variable name="myRegex"> 
    <xsl:value-of select="a\\d{2}\\-[a-zA-Z]+" /> 
    </xsl:variable> 

但我正在逐漸語法錯誤,我試圖轉義字符,但沒有找到任何解決方案尚未

+1

你嘗試單引號和斜槓嗎? () –

+1

我認爲您使用雙反斜槓是因爲使用其他主機語言。在C或Java中,字符串文字中的反斜線需要轉義爲「\\」。 XML或XSLT中的情況並非如此,反斜槓沒有特殊含義。 –

回答

1

您的變量的值必須是一個字符串,所以需要引用它的select裏面...

<xsl:variable name="myRegex" select="'a\d{2}-[a-zA-Z]+'"/> 

像現在的凝固酶原ssor正試圖將您的select評估爲XPath表達式。

另請注意,如果您使用XSLT 1.0,則必須使用extension function(以及支持它的處理器)。

這裏有一個2.0例子...

XML輸入

<doc> 
    <test>a23-abcefghijk</test> 
    <test>qwerty</test> 
</doc> 

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:variable name="myRegex" select="'a\d{2}-[a-zA-Z]+'"/> 

    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="test"> 
    <test matches="{if (matches(.,$myRegex)) then 'yes' else 'no'}"> 
     <xsl:value-of select="."/> 
    </test> 
    </xsl:template> 

</xsl:stylesheet> 

XML輸出

<doc> 
    <test matches="yes">a23-abcefghijk</test> 
    <test matches="no">qwerty</test> 
</doc> 
+0

我正在使用XSLT 1.0 ..現在我在樣式表中使用類似xmlns:javaString =「xalan://java.lang.String」的樣式 和調用匹配函數 – Furqan

+0

@Furqan - 你仍然有麻煩嗎?如果是這樣,請更新您的問題與更多細節。 –

+0

其解決謝謝:) – Furqan

0
<xsl:variable name="myRegex"> 
    <xsl:value-of select="regExp:match('This is a test string', '(a\d{2}\-[a-zA-Z]+)', 'g'" /> 
</xsl:variable>