Re: [xsl] if a condition is not met

Subject: Re: [xsl] if a condition is not met
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Tue, 15 May 2007 18:15:27 +0200
Hi David,

Perhaps something like this (XSLT 2.0), taken all your requirements, if I understood them correctly, but it's a rather naive attempt, I'm sure it can be far simpler than this (I loathe the repeated logic...):


<xsl:template match="/" name="main">
<xsl:for-each-group
select="times/time[position() != last()]"
group-starting-with="node()[@time = ../../cells/cell/(@startTime, @endTime)]">


<!-- all starting times -->
<xsl:apply-templates select="current-group()[1][@time = ../../cells/cell/@startTime]" mode="start-time" />
<!-- all times 'during' an event -->
<xsl:apply-templates select="current-group()[1][@time = ../../cells/cell/@startTime]/current-group()[position() > 1]" />
<!-- all times that form a gap -->
<xsl:apply-templates select="current-group()[1][not(@time = ../../cells/cell/@startTime)]/current-group()" mode="gap-time" />
</xsl:for-each-group>
</xsl:template>


<xsl:template match="time" mode="start-time">
<start-time time="{.}" user="{../../cells/cell[@startTime = current()/@time]}" />
</xsl:template>


<xsl:template match="time">
   <occupied-time time="{.}" />
</xsl:template>

<xsl:template match="time" mode="gap-time">
   <gap time="{.}" />
</xsl:template>


This will output, taken your input, the following XML:


<start-time time="11:00" user="Marquette"/>
<occupied-time time="11:30"/>
<occupied-time time="12:00"/>
<occupied-time time="12:30"/>
<gap time="01:00"/>
<gap time="01:30"/>
<start-time time="02:00" user="Birnbaum"/>
<occupied-time time="02:30"/>
<start-time time="03:00" user="Konsko"/>
<occupied-time time="03:30"/>
<start-time time="04:00" user="Frasier"/>
<occupied-time time="04:30"/>


Since you say that you only need the 'gap' time, you can suffice with the for-each-group and the following as its content, with or without a mode, depending on what you want:


<xsl:apply-templates select="current-group()[1][not(@time = ../../cells/cell/@startTime)]/current-group()" />


Can someone, btw, elaborate on the following: Why is it that a statement like current-group()[some-predicate]/following-sibling::* will return *all* following sibling and not only the elements that are part of the current group? My guess is that current-group() retains its bond with the original tree, is that correct?


To workaround this, I know that you can treat the current-group() results as a document-node when you copy it into a variable and go from there.

Cheers,
-- Abel Braaksma




David J Birnbaum wrote:


for each time element
is there a cell that occurs during (but does not begin) that time?
if any cell meets that condition, do nothing at all within that pass through the time loop
if no cell meets that condition, do something, but do it only once (total) during that pass through the time loop

Current Thread