[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: An approach to XSLT programming, was Re: Dy

Subject: Re: An approach to XSLT programming, was Re: Dynamic variables?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 29 Nov 2010 18:16:21 -0500
Re:  An approach to XSLT programming
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.