Re: [xsl] When to use conditional constructions?

Subject: Re: [xsl] When to use conditional constructions?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sun, 30 Mar 2014 15:10:37 -0700
On Sun, Mar 30, 2014 at 1:52 PM, John Lumley <john@xxxxxxxxxxxx> wrote:
> And for some cases (such as in my 'pagination' example) whilst the
conditions can be described as mutually exclusive, they are really compounds -
e.g. is-page-break, will-fit and not(is-page-break), not(is-page-break or
will-fit) and can-be-broken etc. which are most coherently described as a
priority sequence of cases

Here is one way to do it -- and it is likely that this can be further
simplified:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:xs="http://www.w3.org/2001/XMLSchema";>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:param name="pNum" select="6"/>

 <xsl:variable name="vConds" as="xs:boolean+">
   <xsl:sequence select="$pNum mod 12 eq 0"/>
   <xsl:sequence select="$pNum mod 10 eq 0"/>
   <xsl:sequence select="$pNum mod 9 eq 0"/>
   <xsl:sequence select="$pNum mod 8 eq 0"/>
   <xsl:sequence select="$pNum mod 6 eq 0"/>
   <xsl:sequence select="$pNum mod 4 eq 0"/>
   <xsl:sequence select="$pNum mod 3 eq 0"/>
   <xsl:sequence select="$pNum mod 2 eq 0"/>
  </xsl:variable>

  <xsl:variable name="vTargets"
as="element()+"><x1/><x2/><x3/><x4/><x5/><x6/><x7/><x8/></xsl:variable>

  <xsl:variable name="vFirstTrueIndex" select="index-of($vConds,
true())[1]" as="xs:integer"/>

  <xsl:template match="/">
    <xsl:apply-templates select="$vTargets[$vFirstTrueIndex]"/>
  </xsl:template>

  <xsl:template match="x1">Multiple of 12</xsl:template>
  <xsl:template match="x2">Multiple of 10</xsl:template>
  <xsl:template match="x3">Multiple of 9</xsl:template>
  <xsl:template match="x4">Multiple of 8</xsl:template>
  <xsl:template match="x5">Multiple of 6</xsl:template>
  <xsl:template match="x6">Multiple of 4</xsl:template>
  <xsl:template match="x7">Multiple of 3</xsl:template>
  <xsl:template match="x8">Multiple of 2</xsl:template>
</xsl:stylesheet>

When this transformation is executed, the wanted, correct result is produced:

    Multiple of 6


--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread