RE: [xsl] Shorter, better, nicer XSLT

Subject: RE: [xsl] Shorter, better, nicer XSLT
From: "McNally, David" <David.McNally@xxxxxxxxxx>
Date: Fri, 15 Mar 2002 14:13:49 -0500
I don't think I would recommend this, but you could replace all the XSLT
template complexity with XPATH complexity.  I did a few tests and it seemed
to work, but there may well be subtle problems.  I don't know if this
approach would be more or less efficient, but it is more difficult to read
and maintain, and I send it only for amusement.  

Basically it tries to address the fact that you can't just say
parrilla[@fecha=$fecha], because when $fecha is '*' you don't get all
parrilla nodes regardless of date.  It arranges for the left hand side of
the equality test to be equal to "*" when $fecha equals "*", and to be equal
to @fecha otherwise - so you get the same effect.

<xsl:template match="programacion">
	<xsl:apply-templates select="
	parrilla[
		(
			concat
			(
				substring($fecha,1,($fecha = '*'))
				,
				substring(@fecha,1,	(($fecha != '*') *
string-length($fecha)))
			)
			=$fecha
		)
		and
		(
			concat
			(
				substring($canal,1,($canal = '*'))
				,
				substring(@canal,1,(($canal != '*') *
string-length($canal)))
			)
			=$canal
		)
		]/evento[contains(.,$busca)]">
		<xsl:sort select="../@fecha"/>
		<xsl:sort select="@hora"/>
	</xsl:apply-templates>
</xsl:template>

Disfrute.
David.
--
David McNally            Moody's Investors Service
Software Engineer        99 Church St, NY NY 10007 
David.McNally@xxxxxxxxxx            (212) 553-7475 


> -----Original Message-----
> From: Antonio Bueno [mailto:atnbueno@xxxxxxxxxx]
> Sent: Thursday, March 14, 2002 5:07 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Shorter, better, nicer XSLT
> 
> 
> Hello,
> 
> Some time ago I wrote a transformation to search text in Sat TV
> program listings. I just wanted it to work, and it did. But I didn't
> really liked how it ended. Too much almost-the-same almost-repeated
> code.
> 
> The transformation searched ("buscar" in Spanish) for the text but it
> also allowed to restrict the search by date ("fecha" in Spanish)
> and/or channel ("canal" in Spanish).
> 
> I finally ended doing each combination separatedly
> (any_date+any_channel, one_date+any_channel, any_channel+one_date and
> one_date+one_channel). And repeated paths everywhere.
> 
> I've included a striped down version of the XML and the XSLT files for
> reference.
> 
> Do you have any ideas to do it better? I'd appreciate any sugestion in
> simplicity or performance and I'll humbly accept any reprimand on my
> lack of XSLT knowledge :)
> 
> Oh, and if someone wants to see the "full version" work, you have it
> at http://mundoplus.tv/programacion/buscador.asp
> 
> Thanks in advance and
> greetings from Spain,
>  Antonio

[snip]

>   <xsl:template match="programacion">
>   <xsl:if test="$busca!=''">
>     <xsl:if test="($canal!='*')and($fecha!='*')">
>     <xsl:variable name="eventos">
>       <xsl:value-of
> select="count(parrilla[(@canal=$canal)and(@fecha=$fecha)]/even
> to[contains(.,$busca)])"/>
>     </xsl:variable>
>     <xsl:if test="$eventos &lt;= 400">
>       <xsl:apply-templates
> select="parrilla[(@canal=$canal)and(@fecha=$fecha)]/evento[con
> tains(.,$busca)]">
>       <xsl:sort select="../@fecha"/>
>       <xsl:sort select="@hora"/>
>       </xsl:apply-templates>
>     </xsl:if>
>     </xsl:if>
>     <xsl:if test="($canal!='*')and($fecha='*')">
>     <xsl:variable name="eventos">
>       <xsl:value-of 
> select="count(parrilla[@canal=$canal]/evento[contains(.,$busca)])"/>
>     </xsl:variable>
>     <xsl:if test="$eventos &lt;= 400">
>       <xsl:apply-templates 
> select="parrilla[@canal=$canal]/evento[contains(.,$busca)]">
>       <xsl:sort select="../@fecha"/>
>       <xsl:sort select="@hora"/>
>       </xsl:apply-templates>
>     </xsl:if>
>     </xsl:if>
>     <xsl:if test="($canal='*')and($fecha!='*')">
>     <xsl:variable name="eventos">
>       <xsl:value-of 
> select="count(parrilla[@fecha=$fecha]/evento[contains(.,$busca)])"/>
>     </xsl:variable>
>     <xsl:if test="$eventos &lt;= 400">
>       <xsl:apply-templates 
> select="parrilla[@fecha=$fecha]/evento[contains(.,$busca)]">
>       <xsl:sort select="../@fecha"/>
>       <xsl:sort select="@hora"/>
>       </xsl:apply-templates>
>     </xsl:if>
>     </xsl:if>
>     <xsl:if test="($canal='*')and($fecha='*')">
>     <xsl:variable name="eventos">
>       <xsl:value-of 
> select="count(parrilla[@fecha=$fecha]/evento[contains(.,$busca)])"/>
>     </xsl:variable>
>     <xsl:if test="$eventos &lt;= 400">
>       <xsl:apply-templates 
> select="parrilla/evento[contains(.,$busca)]">
>       <xsl:sort select="../@fecha"/>
>       <xsl:sort select="@hora"/>
>       </xsl:apply-templates>
>     </xsl:if>
>     </xsl:if>
>   </xsl:if>
>   </xsl:template>


---------------------------------------

The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission.  If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited.  If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments.  Thank you.

Every effort is made to keep our network free from viruses.  You should, however, review this e-mail message, as well as any attachment thereto, for viruses.  We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.


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


Current Thread