Re: [xsl] Problem with xsl:template using XSLT 1.0

Subject: Re: [xsl] Problem with xsl:template using XSLT 1.0
From: Florent Georges <lists@xxxxxxxxxxxx>
Date: Wed, 5 Dec 2007 17:14:22 +0100 (CET)
Andrew Welch wrote:

  Hi Andrew,

> If I've followed the thread I think you're discussing the
> choice of filtering the nodes to be processed using a
> predicate:

> <xsl:apply-templates select="some-nodes[some-filter]"/>

> with:

> <xsl:template match="some-nodes"> ....

> or using a filter at the template match level with no-op
> templates:

> <xsl:apply-templates/>
> <xsl:template match="some-nodes"/>
> <xsl:template match="some-nodes[some-filter]">....

> ?

  More or less.  More precisely, I responded to the
following advice:

    Remember that your <xsl:template/> elements should match
    fairly generically (match="player" in this case), and
    your <xsl:apply-templates/> elements should be where you
    narrow down your selection.

  I just say that in general, I would give instead the
opposite advice.  Of course, there is place for both
patterns, and I expect any not-simple-toy-stylesheet to use
both.  With experience, when using one or the other become
quite obvious.

  But the beginner with a typical computer science studies
background - that is, used to imperative languages - would
rather tend I think to use the former pattern.  Because the
rules dictionnary-driven processing in the heart of XSLT is
quite different from what he knows.

  Here is my rule of thumb to decide when I use one or the
other (this decision is quite unconscious):

    - the most important factor is which component has the
      responsability to choose to do something or not with a
      node (is it obvious that the template rule for an
      element knows which children to select, or instead the
      template rules for some nodes are obvious to say
      "don't do nothing with this node");

    - sometimes your template is highly structured, and the
      output structure doesn't reflect the input structure
      at all.  In such a case, you'll just pick up the exact
      nodes you want at the exact place you need them.  But
      sometimes this is not so obvious: for example if you
      want to create a table of content and an index while
      transforming an input to a visual XML language, it
      would be tempting to select the nodes to include in
      the ToC, then those in the body, then those in the
      index, but maybe it should be more maintainable to use
      different modes and walk your whole input three times;

    - if you want to be able to overload what nodes are
      selected or not, the later pattern is more adapted;

    - if which template rule actually apply the template is
      relevant to selecting the nodes, maybe the former is
      more adapted.  But once again, a dedicated mode could
      be the answer;

    - if the choice is really not so obvious, I tend to use
      the former pattern, maybe because this is faster to
      type while writing down the algorithm you have in
      mind, and that tends to minimize the number of modes
      you use, letting the possibility to use the later
      pattern if more flexibility is needed later...



Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail

Current Thread