Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 3.04 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to implement XSLT tokenize function?
  2. <xsl:stylesheet
  3.     version="1.0"
  4.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  5.     xmlns:func="http://exslt.org/functions"
  6.     xmlns:exsl="http://exslt.org/common"
  7.     xmlns:my="http://mydomain.com/">
  8.  
  9.     <func:function name="my:tokenize">
  10.         <xsl:param name="string"/>
  11.         <xsl:param name="separator" select="'|'"/>
  12.         <xsl:variable name="item" select="substring-before(concat($string,$separator),$separator)"/>
  13.         <xsl:variable name="remainder" select="substring-after($string,$separator)"/>
  14.         <xsl:variable name="tokens">
  15.             <token><xsl:value-of select="$item"/></token>
  16.             <xsl:if test="$remainder!=''">
  17.                 <xsl:copy-of select="my:tokenize($remainder,$separator)"/>
  18.             </xsl:if>
  19.         </xsl:variable>
  20.         <func:result select="exsl:node-set($tokens)"/>
  21.     </func:function>
  22.  
  23.     <xsl:template match="/">
  24.         <xsl:copy-of select="my:tokenize('a|b|c')"/>
  25.     </xsl:template>
  26.  
  27. </xsl:stylesheet>
  28.        
  29. <token>a</token><token>b</token><token>c</token>
  30.        
  31. abc
  32.        
  33. <xsl:stylesheet version="1.0"
  34.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  35.     xmlns:str="http://exslt.org/strings"
  36.     extension-element-prefixes="str"
  37.     …
  38.        
  39. <select name="months">
  40.     <xsl:for-each select="str:tokenize('1,2,3,4,5,6,7,8,9,10,11,12', ',')">
  41.         <xsl:element name="option">
  42.             <xsl:attribute name="value">
  43.                 <xsl:value-of select="."/>
  44.             </xsl:attribute>
  45.             <xsl:value-of select="."/>
  46.         </xsl:element>
  47.     </xsl:for-each>
  48. </select>
  49.        
  50. <xsl:template name="tokenize">
  51.   <xsl:param name="string"/>
  52.   <xsl:param name="separator" select="'|'"/>
  53.  
  54.   <xsl:choose>
  55.     <xsl:when test="contains($string,$separator)">
  56.       <token>
  57.         <xsl:value-of select="substring-before($string,$separator)"/>
  58.       </token>
  59.       <xsl:call-template name="tokenize">
  60.         <xsl:with-param name="string" select="substring-after($string,$separator)"/>
  61.       </xsl:call-template>
  62.     </xsl:when>
  63.     <xsl:otherwise>
  64.       <token><xsl:value-of select="$string"/></token>
  65.     </xsl:otherwise>
  66.   </xsl:choose>
  67. </xsl:template>
  68.        
  69. <xsl:call-template name="tokenize">
  70.   <xsl:with-param name="string" select="'a|b|c'"/>
  71. </xsl:call-template>
  72.        
  73. <xsl:stylesheet version="1.0"
  74.  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  75.  xmlns:ext="http://exslt.org/common">
  76.  
  77.    <xsl:import href="strSplit-to-Words.xsl"/>
  78.    <xsl:output indent="yes" omit-xml-declaration="yes"/>
  79.  
  80.     <xsl:template match="/">
  81.       <xsl:variable name="vwordNodes">
  82.         <xsl:call-template name="str-split-to-words">
  83.           <xsl:with-param name="pStr" select="/"/>
  84.           <xsl:with-param name="pDelimiters"
  85.                           select="', &#9;&#10;&#13;'"/>
  86.         </xsl:call-template>
  87.       </xsl:variable>
  88.  
  89.       <xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
  90.     </xsl:template>
  91.  
  92.     <xsl:template match="word">
  93.       <xsl:value-of select="concat(position(), ' ', ., '&#10;')"/>
  94.     </xsl:template>
  95. </xsl:stylesheet>
  96.        
  97. <t>out, of
  98.  luck</t>
  99.        
  100. 1 out
  101. 2 of
  102. 3 luck