Re: [xsl] An approach to XSLT programming, was Re: [xsl] Dynamic variables?

Subject: Re: [xsl] An approach to XSLT programming, was Re: [xsl] Dynamic variables?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 29 Nov 2010 18:16:21 -0500
Dave,

At 03:22 AM 11/29/2010, you wrote:
On Sun, 28 Nov 2010 17:52:03 +0000
Michael Kay <mike@xxxxxxxxxxxx> wrote:

> In your rationale you say "
>
> I want to remember those "style" definitions so I can emit appropriate
> code when they are referenced.
>
> Use of temporal language like this ("remember", "when") is always
> dangerous with XSLT; it's best to avoid thinking about XSLT
> computation as having a particular temporal order.

In the opposite case, we hear David C sometimes almost repeat
the words of the OP and derive code from it?

Are there ways that you 'express the problem' that make it easier to
write code?

Yes: in terms of the tree.


In this case "I want to process a node based on conditions defined on another node associated with it by means of labels carried on attributes".

This is spatial rather than temporal; in training ourselves to think this way, it helps to keep in mind that the tree traversal can happen in any order (temporally), so the problem is the same whether the node associations look "backward" or "forward".

The solution (as suggested) is to use a key to retrieve the associated node -- in this case, probably to apply templates to it.

So (if this problem demands HTML/CSS):
<styles>
<style name="basic" typeface="Optima" weight="normal" slant="roman"/>
<style name="emphatic" typeface="Optima" weight="bold" slant="italic"/>
</styles>
. . .
<p style="basic">This paragraph contains some <span
style="emphatic">emphatic</span> text.</p>

<xsl:key name="style-by-name" match="style" use="@name"/>


<xsl:template match="p | span">
<xsl:copy>
<xsl:apply-templates select="key('style-by-name',@style)" mode="css-style"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>


<xsl:template match="style" mode="css-style">
  <xsl:attribute name="style">
    <xsl:apply-templates select="@*" mode="css-style"/>
  <xsl:attribute>
</xsl:template>

<xsl:template match="@typeface" mode="css-style">
  <xsl:text>font-family: </xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>;</xsl:text>
</xsl:template>

etc.

It's conceptually neat, and easy to maintain. While, if you think temporally, it seems inefficient (the same nodes get visited repeatedly), it also relies on XSLT constructs that admit of a high degree of optimization in the processor.

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