How to check if xml textnode has Chinese characters with RegEx in a XSLT
On this website http://gskinner.com/RegExr/ (which is a RegEx test website) this regex match works
Match:
[^\x00-\xff]
test123 或元件数据不可用
But if I have this input XML:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<node>test123 或元件数据不可用</node>
</root>
and I try this XSLT 2.0 stylesheet with Saxon 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>
Saxon 9 gives me following error output:
FORX0002: Error at character 3 in regular expression "[^\x00-\xff]": invalid escape sequence
Failed to compile stylesheet. 1 error detected.
How to check for chinese characters inside XSLT 2.0 ?
The regex dialect supported by XPath is based on that defined in XSD: you can find full specifications in the W3C documents, or if you prefer something more readable, in my XSLT 2.0 Programmer's Reference. Don't assume that all regex dialects are the same. There's no \x
escape in XPath regexen because it's designed for embedding in XML which already offers &#xHHHH;
.
Rather than using a hex range you might find it more convenient to use a named Unicode block, for example \p{IsCJKUnifiedIdeographs}
.
See also What's the complete range for Chinese characters in Unicode?
With the help from Michael Kay I can answer my question myself. Thanks Michael! The solution works but in my opinion this long Unicode ranges do not look very pretty.
This XSLT will print a text message if any Chinese character were found with regular expressions in the given 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(.,'[一-鿿㐀-䷿𠀀-𪛟豈-﫿丽-𯨟]')">
<xsl:text>Text has chinese characters!</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Solution with named Unicode block:
<?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>
精彩评论