XML/XSLT stylesheet/analyze string

Материал из Web эксперт
Перейти к: навигация, поиск

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>&#xA;+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>&#xA;   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



matching-substring

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-1111</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}})(-|\.)([0-9]{{3}})(-|\.)([0-9]{{4}})">
        <xsl:matching-substring>
          <xsl:text>&#xA;+1 (</xsl:text>
          <xsl:value-of select="regex-group(1)"/>
          <xsl:text>) </xsl:text>
          <xsl:value-of select="regex-group(3)"/>
          <xsl:text>-</xsl:text>
          <xsl:value-of select="regex-group(5)"/>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
          <xsl:text>&#xA;   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
+1 (111) 111-1111
+1 (111) 111-1111
   Unrecognized phone number: +11 111-1111



non-matching-substring and matching-substring

File: Data.xml
<?xml version="1.0"?>
 
<start_date xmlns="http://www.wbex.ru">1999-07-25</start_date>
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:analyze-string select=""&#x212A;"" regex="k" flags="i">
      <xsl:matching-substring>
        <xsl:text>It matches!</xsl:text>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:text>It doesn"t match!</xsl:text>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>
</xsl:stylesheet>
Output:
It matches!