xsl-list
[Top] [All Lists]

Re: template's match-set ? (feature request?)

2002-09-06 06:19:17
Hi Rob,

I was looking for the following functionality in the draft for
XSLT2, but could not find it. It seems like it is possible.

Anyway, what I want to do is have a 'match-set' (sort of like
attribute-set) that I can 'use-match-set' on various templates with
different modes.

Hmm... no, there isn't anything similar to what you're proposing in
XSLT 2.0. However, I can think of a couple of ways that you could
achieve what you want to achieve.

The first will work in both XSLT 1.0 and XSLT 2.0. You set up a key
that matches the things that you want to match, and indexes them by a
static string (which could in fact be empty, but let's use something a
bit more meaningful):

<xsl:key name="match-set" match="article | faq | job"
         use="'content-pieces'" />

You can then create templates that match these elements by using the
key() function in the match attribute of the template. For example:

<xsl:template match="key('match-set', 'content-pieces')"
              mode="mode-1">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="key('match-set', 'content-pieces')"
              mode="mode-2">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="key('match-set', 'content-pieces')"
              mode="mode-n">
  <xsl:apply-templates/>
</xsl:template>

These templates will match anything that's matched by the
'match-set' key and takes the value 'content-pieces'. You could have
other key definitions, also for the 'match-set' key, that matched
different kinds of nodes and gave different values for them if you
wanted.

The second, in XSLT 2.0 or using func:function from EXSLT with XSLT
1.0, is to create a function that returns true if a node is part of
the set that you want to match and false if not. Something like:

<xsl:function name="my:is-content-piece">
  <xsl:param name="node" select="/.." />
  <xsl:result select="self::article or self::faq or self::job" />
</xsl:function>

and then call that function from a predicate in the match patterns:

<xsl:template match="*[my:is-content-piece(.)]" mode="mode-1">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="*[my:is-content-piece(.)]" mode="mode-2">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="*[my:is-content-piece(.)]" mode="mode-n">
  <xsl:apply-templates/>
</xsl:template>

These templates match any element for which the my:is-content-piece()
extension function returns true, which is only the case for article,
faq or job elements.

I must say that I do like the first method, but perhaps it's a little
hacky. It's more obvious how the second method works.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list