Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Transforming strings with regular expressions (7/8) >

Creation of text nodes from a string with regular expressions - function analyze-string()

The element xsl:analyze-string creates a sequence of text nodes from an input string. The parts of text to be selected are given by a regular expression in regex attribute.

The element xsl:matching-substring generates a text node from every string matched by the regex. Look at xsl:non-matching-substring for using regex as a separator.

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/aaa">
                  <xsl:analyze-string  select="bbb"
                              regex="\S+">
                        <xsl:matching-substring>
                              <aaa>
                                    <xsl:value-of  select="."/>
                              </aaa>
                        </xsl:matching-substring>
                  </xsl:analyze-string>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb>How do you do?</bbb>
      </aaa>
Output

      <aaa>How</aaa>
      <aaa>do</aaa>
      <aaa>you</aaa>
      <aaa>do?</aaa>


Previous chapter: Functions operating on strings
Next chapter: Number Formatting
Previous page: Creating text nodes by regular expressions - function tokenize
Next page: Creation of text nodes from a string with a regular expression separator