xsl:analyze-string

  1. analyze-string_1-input.xml

  2. <?xml version="1.0"?>
  3. <book>
  4.   <title>once upon a time in mexico</title>
  5. </book>
  1. analyze-string_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  4.   <xsl:output indent="yes"/>
  5.   <xsl:template match="/">    
  6.     <xsl:variable name="x">
  7.       <xsl:analyze-string select="book/title" regex="\w+">
  8.         <xsl:matching-substring>
  9.           <xsl:value-of select="concat(upper-case(substring(., 1, 1)), substring(., 2))"/>
  10.         </xsl:matching-substring>
  11.         <xsl:non-matching-substring>
  12.           <xsl:value-of select="."/>
  13.         </xsl:non-matching-substring>
  14.       </xsl:analyze-string>
  15.     </xsl:variable>
  16.     <book>
  17.       <title>
  18.         <xsl:value-of select="$x"/>
  19.       </title>
  20.     </book>
  21.   </xsl:template>
  22. </xsl:stylesheet>
  1. analyze-string_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <book>
  4.   <title>Once Upon A Time In Mexico</title>
  5. </book>

Comment

We need at least one xsl:matching-substring or one xsl:non-matching-substring. Inside xsl:matching-substring and xsl:non-matching-substring we can have a new xsl:analyze-string, etc. In the example xsl:matching-substring finds all words and make the first letter into upper-case. The xsl:non-matching-substring finds the rest that is the spaces betweeen the words.

Updated 2009-03-19