analyze-string and regular expression
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<phonelist>
<phonenumber>111-111-1111</phonenumber>
<phonenumber>(111) 111-1111</phonenumber>
<phonenumber>111.111.1111</phonenumber>
<phonenumber>111-111-112</phonenumber>
<phonenumber>+11 111-1111</phonenumber>
</phonelist>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="phonelist/phonenumber">
<xsl:analyze-string select="." regex="([0-9]{{3}})-(\p{{Nd}}{{3}})-([0-9]{{4}})">
<xsl:matching-substring>
<xsl:text>
+1 (</xsl:text>
<xsl:value-of select="regex-group(1)"/>
<xsl:text>) </xsl:text>
<xsl:value-of select="regex-group(2)"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="regex-group(3)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:text>
 Unrecognized phone number: </xsl:text>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
+1 (111) 111-1111
Unrecognized phone number: (111) 111-1111
Unrecognized phone number: 111.111.1111
Unrecognized phone number: 111-111-112
Unrecognized phone number: +11 111-1111
Related examples in the same category