Thursday 30 December 2010

Applying XSLT template rules to strings / matching strings

I extremely like template rules in XSLT (I miss them much in XQuery). But it's impossible to match on simple types, only on nodes.
Solution is trivial - use xsl:value-of to construct text node, which can be matched:


  <xsl:variable name="text">

  <xsl:variable as="text()*" name="words">
    <xsl:for-each select="tokenize( normalize-space(.), ' ' )">
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:variable>

<xsl:template match="text()[ starts-with(.,'A') ]" mode="word">
  <A>
    <xsl:copy-of select="."/>
  </A>
</xsl:template>



A very simple and trivial thing, but took some time for me to figure out - writing it here mostly for myself - not to forget.