Re: [xsl] Counting the number of matches

Subject: Re: [xsl] Counting the number of matches
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 9 May 2007 23:29:15 +0100
$ saxon8 hit.xml hit.xsl search=foo
<?xml version="1.0" encoding="UTF-8"?><root>
  <text>This is <font color="blue"><a hit="1"/>foo</font></text>
  <element>
   <sub>This is another <font color="blue"><a hit="2"/>foo</font><br/> and yet more <font color="blue"><a hit="3"/>foo</font> galore</sub>
  </element>
 </root>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:param name="search" select="''"/>
  
  <xsl:template match="/">
    <xsl:variable name="x">
      <xsl:apply-templates/>
    </xsl:variable>
    <xsl:apply-templates mode="b" select="$x"/>
  </xsl:template>

  <xsl:template match="*" mode="#default b">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates mode="#current"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="text()[contains(., $search)]">
    <xsl:analyze-string select="." regex="{$search}(s|ing){{0,1}}">
      <xsl:matching-substring>
	<font color="blue"><a><hit/></a><xsl:value-of
	select="."/></font>
      </xsl:matching-substring>
      <xsl:non-matching-substring><xsl:value-of
      select="."/></xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>

  <xsl:template match="hit" mode="b">
      <xsl:attribute name="hit">
	<xsl:number level="any"/>
      </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

David

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 
________________________________________________________________________

Current Thread