2011-07-07 10 views
4

在這個網站上http://gskinner.com/RegExr/(這是一個正則表達式測試網站)這個表達式匹配中國的字符,正則表達式的工作 比賽: [^\x00-\xff]
示例文字:test123 或元件數據不可用如何檢查是否XML textnode有一個XSLT

但如果我有這個輸入XML:

<?xml version="1.0" encoding="UTF-8" ?> 
<root> 
    <node>test123 或元件數據不可用</node> 
</root> 

,我試試這個XSLT 2.0樣式表與撒克遜9:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/root/node"> 
    <xsl:if test="matches(., '[^\x00-\xff]')"> 
     <xsl:text>Text has chinese characters!</xsl:text> 
    </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

撒克遜9給了我下面的錯誤輸出:

FORX0002: Error at character 3 in regular expression "[^\x00-\xff]": invalid escape sequence 
    Failed to compile stylesheet. 1 error detected. 

如何檢查中國文字裏XSLT 2.0嗎?

+0

也許嘗試字符引用來代替, '� - ÿ'? –

回答

3

看到的幫助下,從邁克爾·凱,我可以回答我的問題我自己。謝謝邁克爾! 解決方案的工作,但在我看來,這漫長的Unicode範圍不看很漂亮。

這XSLT將打印文本信息,如果任何一箇中國字被發現與給定XML正則表達式:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/root/node"> 
    <xsl:if test="matches(.,'[&#x4E00;-&#x9FFF;&#x3400;-&#x4DFF;&#x20000;-&#x2A6DF;&#xF900;-&#xFAFF;&#x2F800;-&#x2FA1F;]')"> 
     <xsl:text>Text has chinese characters!</xsl:text> 
    </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

解決方案與命名的Unicode塊:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/root/node"> 
    <xsl:if test="matches(., '[\p{IsCJKUnifiedIdeographs}\p{IsCJKUnifiedIdeographsExtensionA}\p{IsCJKUnifiedIdeographsExtensionB}\p{IsCJKCompatibilityIdeographs}\p{IsCJKCompatibilityIdeographsSupplement}]')"> 
     <xsl:text>Text has chinese characters!</xsl:text> 
    </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 
3

與XPath支持正則表達式方言是基於在XSD定義的:你可以找到完整的規格在W3C的文檔,或者如果你喜歡的東西更具有可讀性,我在XSLT 2.0程序員參考。不要以爲所有的正則表達式都是相同的。有沒有\x逃生XPath中使用regexen,因爲它設計用於XML它已經提供&#xHHHH;嵌入。

而不是使用十六進制範圍你可能會發現它更方便地使用一個名爲Unicode的塊,例如\p{IsCJKUnifiedIdeographs}

What's the complete range for Chinese characters in Unicode?

+0

感謝您的支持!有了你的幫助,我可以幫助自己:-)將在下面發佈答案。似乎Unicode並不是世界上最簡單的東西。 – therealmarv

相關問題