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

Re: Need help for XSL beautifaction

Subject: Re: Need help for XSL beautifaction
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 17 May 2006 14:46:08 -0400
simply beautif
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[@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[@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@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.