Re: [xsl] Matching a local scope name

Subject: Re: [xsl] Matching a local scope name
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 15 Sep 2010 12:42:32 -0400
Hi David,

At 08:29 AM 9/15/2010, Andrew wrote:
> Suppose I want a template to just match the later
>
> <template match="section">
>
> Would match all three (?)
>
> <template match="master/section" >
> might do the trick but suppose there is another "master" locally defined
> elsewhere that I DONT want to match.

You have 2 options, 1 - make the match patterns more specific,  or 2 -
be more selective when you apply-templates.  Your template will only
get used if you call apply-templates, or let the inbuilt templates
kick in and call apply-templates.

As Andrew indicates, to crack this nut you really have to understand the difference between template matching and selecting for processing.


A template will be called only if it is the best match (in the given mode) for a node selected for processing using apply-templates. (Or if it is called by name, of course.)

So if you want to avoid processing certain nodes, one option is simply to see that they are never selected.

However, if a node is selected, whether it matches is determined by logic like the following: given pattern P and node N, is there some node context C from which, using P as an XPath expression, node N can be selected? (And then keep in mind that due to restrictions on pattern syntax, not just any XPath can be used as a pattern. One consequence of the restrictions is that C can only appear among ancestors of any candidate node N.)

You'll see this means definitively that in no case will "master/section" ever match a 'master' element, since there is never a node (some context C) from which any 'master' (as N) can be selected using "master/section" (expression P). (For any context C without exception, "master/section" will select either 'section' elements, or nothing at all: we will get any 'section' elements inside 'master' children of C, so those are the nodes matching the pattern.)

As to local vs global, templates are always global in scope. (As Wolfgang suggested, modes are another way of qualifying this). So if you have a template that matches "master/section", it will never match 'master' elements, but it might match a 'section' element anywhere at all, as long as its parent is 'master'. (While a 'section' element inside, say, 'subordinate', couldn't ever be selected using the path "master/section", so it won't match that pattern.)

When this causes issues, we just apply more XPath to discriminate more finely.

Learning to choreograph the dance between select expressions and match patterns is the essence of XSLT.

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
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