Re: [xsl] Need help for XSL beautifaction

Subject: Re: [xsl] Need help for XSL beautifaction
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 17 May 2006 14:46:08 -0400

At 01:27 PM 5/17/2006, you wrote:
<xsl:stylesheet xmlns:xsl=""; xmlns:fo=""; version="1.0">
<!-- Define Text-Modules -->
<xsl:variable name="text-1">This is long text number one.</xsl:variable>
<xsl:variable name="text-2">This is an other very long text.</xsl:variable>
<xsl:variable name="text-3">This is my third text without sense.</xsl:variable>
<!-- Hard-coded text-module selection -->
<xsl:when test="textNo = 'text-1'">
<xsl:copy-of select="$text-1"/>
<xsl:when test="textNo = 'text-2'">
<xsl:copy-of select="$text-2"/>
<xsl:when test="textNo = 'text-3'">
<xsl:copy-of select="$text-3"/>

I dont like the the hard-coded tests to the value of the element "textNo", hope there is a way directly to use the <xsl:copy-of> to the variable with the name, which is the same as the value in the element "textNo".

There is a nice application of the document() function, which is used to call in data from an external document, very useful for situations like this. document('') (with an empty string as argument) will return the stylesheet itself, as a tree of nodes that can be queried or even processed.

This makes it straightforward to create and use internal lookup tables in a stylesheet:

<xsl:stylesheet version="1.0"

<!-- Define Text-Modules -->
<xsl:variable name="snip-lookup">
 <snip name="text-1">This is long text number one.</snip>
 <snip name="text-2">This is an other very long text.</snip>
 <snip name="text-3">This is my third text without sense.</snip>

<xsl:variable name="snippets"

The variable $snips now contains a set of nodes, namely all the 'snip' elements in the variable.

(Note this is a tortuous workaround for something that would be very simple in XSLT 2.0, namely $snip-lookup/snip.)

Then instead of a choose/when/otherwise we can simply say

<xsl:apply-templates select="$snippets[@name=current()/textNo]"/>

(or value-of, or whatever: it's the select expression that matters here.)

This is all documented in the XSL FAQ under, I believe, "lookup tables".

Improve performance of the lookup using keys.


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
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