[xsl] Counting the number of matches

Subject: [xsl] Counting the number of matches
From: "Spencer Tickner" <spencertickner@xxxxxxxxx>
Date: Wed, 9 May 2007 15:00:10 -0700
Hi List,

Please don't yell at me too much ;) I know variables can't be
incremented and kinda have a vague idea that xslt processors don't
neccessarly process templates in order, but I'm still wondering if
something like this is possible.

I'm using 2.0, and want to get reference to a "hit" within a documents
text for later use:

XSLT Snippet

<xsl:param name="search" select="''"/>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
 <xsl:apply-templates/>
</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="<!-- Something Here -->"/><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>

If $search was "foo" is there a way to get the a@hit attribute to
contain the pseudo-position() of that hit in the document, no matter
which level or element "foo" appears in?

so with XML Snippet

<?xml version="1.0"?>
<root>
 <text>This is foo</text>
 <element>
  <sub>This is another foo<br/> and yet more foo galore</sub>
 <element>
</root>

could get:


<?xml version="1.0"?> <root> <text>This is <a hit="1">foo</a></text> <element> <sub>This is another <a hit="2">foo</a><br/> and yet more <a hit="3">foo</a> galore</sub> <element> </root>


Thanks,


Spencer

Current Thread