xsl-list
[Top] [All Lists]

Re: [xsl] Need help for XSL beautifaction

2006-05-17 11:47:56
Christian,

At 01:27 PM 5/17/2006, you wrote:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:fo="http://www.w3.org/1999/XSL/Format"; 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:choose>
 <xsl:when test="textNo = 'text-1'">
   <xsl:copy-of select="$text-1"/>
 </xsl:when>
 <xsl:when test="textNo = 'text-2'">
   <xsl:copy-of select="$text-2"/>
 </xsl:when>
 <xsl:when test="textNo = 'text-3'">
   <xsl:copy-of select="$text-3"/>
 </xsl:when>
</xsl:choose>

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"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:fo="http://www.w3.org/1999/XSL/Format";>

<!-- 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>

<xsl:variable name="snippets"
  select="document('')/*/xsl:variable[(_at_)name='snip-lookup']/snip"/>

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[(_at_)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.

Regards,
Wendell



======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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
======================================================================


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--