RE: [xsl] First occurrence of glossary term in whole document

Subject: RE: [xsl] First occurrence of glossary term in whole document
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 30 Jul 2008 18:12:43 -0400

At 02:06 PM 7/30/2008, you wrote:
Ooops, that isn't true. I can't pass the result document down because the
variable isn't updated until after the full file is processed thereby
allowing multiple references to creep in.

Right. Indeed, the reference you are checking on behalf of would already have been processed too, thereby introducing a circular dependency.

I'll have another go tomorrow using Liam's suggestion. Thanks. R

Yes. The essence of Lee's suggestion is that in a declarative mindset, you don't look for a corresponding case that has already been processed; you look for corresponding cases that should be privileged over the current case by some static rule, such as that it appears earlier.

Wanting just the first case (and thereby, a single case for every class of corresponding cases) is common, and the key-based idiom to do this (which is generally faster than using XPath to inspect the tree) is something like this:

(in your data)

(in your stylesheet)

<xsl:key name="glossterm-by-value" match="glossterm" use="string()"/>

then in a template matching glossterm (in XSLT 1.0):

<xsl:template match="glossterm">
  <xsl:if test="generate-id()=generate-id(key('glossterm-by-value',.)[1])">
    ... only the first glossterm in the document will pass the test ...

in XSLT 2.0 one can test ". is key('glossterm-by-value',.)[1]" which is more legible.

One can also match on that node, but it leaves the glossterms that don't pass the test unmatched.


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread