Re: [xsl] What are these methods?

Subject: Re: [xsl] What are these methods?
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sun, 29 Apr 2001 13:50:45 +0100
Hi Sun-fu,

> Would anyone be kind to direct me to the reference about these
> methods which Jeni specifically mentioned in her XSLT UK 2001
> Report.

These are detailed in my paper from the conference - Sebastian, Dave -
are you intending to put them on the web at all?

> patterns in instructions such as Wendell Piez's method for
> repetition

The Piez's Method is for iterating a number of times. As you know,
there's no way that you can do a for loop in XSLT in the same way as
you would in a procedural programming language - you can only iterate
over a number of nodes. However, what you can do is choose the number
of nodes you iterate over, and then use the position() of that node to
indicate the number of the iteration. So you set up a random set of
nodes (I usually use nodes in the stylesheet itself):

<xsl:variable name="random-nodes" select="document('')//node()" />

Then you pick from them the number that you want, and iterate over
them:

<xsl:for-each select="$random-nodes[position() &lt; $number]">
   ...
</xsl:for-each>

Whatever you want to do, held in the xsl:for-each, is repeated $number
times.

(Note: You can do the same thing with a recursive template, and the
Piez Method can be tricky if you can't find enough random nodes to
use, and can take up a lot of memory if you collect too many random
nodes, but most of the time it's a lot less bother than writing a
recursive template.)

> and David Allouche's method for normalizing strings

The Allouche Method helps you control whitespace in your result. When
the XSLT processor reads in the stylesheet, it strips out any
whitespace-only text nodes (text nodes that are made up purely of
whitespace), but it leaves in any text nodes that have non-whitespace
characters in them.  Usually you'd get around this by wrapping the
text that you actually want added within an xsl:text element.  So for
example:

  <xsl:text>(</xsl:text>
  <xsl:value-of select="$expression" />
  <xsl:text>)</xsl:text>

Rather than doing that, you can use an empty xsl:text element (or
indeed any other XSLT element, but xsl:text is good because it's short
and it doesn't give you any output) to delimit the whitespace that you
don't want.  So instead of the above, I could do:

  <xsl:text />(<xsl:value-of select="$expression" />)<xsl:text />

(Note: The other method for dealing with this kind of situation is to
wrap the text in a concat() in the xsl:value-of, e.g.:

  <xsl:value-of select="concat('(', $expression, ')')" />

That's probably a little more efficient and reduces the size of the
stylesheet node tree, but the Allouche Method is more general.)

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread