Re: [xsl] xsl:choose and xsl:when

Subject: Re: [xsl] xsl:choose and xsl:when
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Thu, 30 Aug 2007 01:16:54 +0200
[somehow this didn't reach the list, retry. probably outdated now though..]

Hi Oryan,

see my comments,

Cheers,
Abel

oryann9 wrote:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="html"/>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>My Phone Book</TITLE>
</HEAD>
<BODY BGCOLOR="BLACK">
<xsl:apply-templates select="//LISTING" />

You probably want just LISTING here. You are selecting every possible LISTING from the root through all the document when you use //LISTING.

</BODY>
</HTML>
</xsl:template>
<xsl:template match="LISTING">
<FONT COLOR="WHITE"><xsl:value-of
select="LAST"/>,
<xsl:value-of select="FIRST"/></FONT>
<FONT COLOR="orange" SIZE="+2"><xsl:value-of
select="PHONE"/>
<xsl:choose>
<xsl:when test="boolean(PHONE/@TYPE)">
(<xsl:value-of select="PHONE/@TYPE" />)

No need for an xsl:choose. In almost all cases that I've seen on this list, xsl:choose is over-used, because it feels so easy (or looks so much like in the other programming languages). Instead, use:

<FONT COLOR="orange" SIZE="+2">
   <xsl:apply-templates select="PHONE" />

then, have twotemplate matches (one for empty or non-existing @TYPE, the
other for all other @TYPE):

<xsl:template match="PHONE[@TYPE='' | not(@TYPE)]" >
   <xsl:value-of select="."/> (HOME)
</

<xsl:template match="PHONE[@TYPE != '']">
   <xsl:value-of select="."/> (<xsl:value-of select="PHONE/@TYPE" />)
</


      </xsl:when>
       <xsl:when test="(PHONE/@TYPE='')">
            (HOME)
      </xsl:when>
      <xsl:otherwise>
           (HOME)
      </xsl:otherwise>
</xsl:choose>
     </FONT>
</xsl:template>
</xsl:stylesheet>

Current Thread