tags:

views:

44

answers:

2
+2  Q: 

XSLT | Choose/When

Here is my code:

<xsl:choose>
    <xsl:when test="$all_alerts[(g:line_abbr = 'MFL') or (g:line_abbr = 'BSL') or (g:line_abbr = 'CCT') or (g:line_abbr = 'NHSL') and (g:problem != 'normal_service')]">
        <b>Subway</b><br /><br />
        <xsl:for-each select="$all_alerts[g:line_abbr = 'MFL']">
            <xsl:if test="g:problem != 'normal_service'"><xsl:value-of select="g:line"/> | <a href="/m/alert/mfl.html">
                 <xsl:if test="g:problem = 'station_advisory'">Station Advisory</xsl:if>
                 <xsl:if test="g:problem = 'stop_advisory'">Stop Advisory</xsl:if>
                 <xsl:if test="g:problem = 'customer_notice'">Customer Notice</xsl:if>
                 <xsl:if test="g:problem = 'service_advisory'">Service Advisory</xsl:if>
                 <xsl:if test="g:problem = 'shuttle_bus'">Shuttle Bus</xsl:if></a><br /><br /></xsl:if>
            <xsl:if test="g:problem = 'normal_service'"></xsl:if>
         </xsl:for-each>

        <xsl:for-each select="$all_alerts[g:line_abbr = 'BSL']">
            <xsl:if test="g:problem != 'normal_service'"><xsl:value-of select="g:line"/> | <a href="/m/alert/bsl.html">
                 <xsl:if test="g:problem = 'station_advisory'">Station Advisory</xsl:if>
                 <xsl:if test="g:problem = 'stop_advisory'">Stop Advisory</xsl:if>
                 <xsl:if test="g:problem = 'customer_notice'">Customer Notice</xsl:if>
                 <xsl:if test="g:problem = 'service_advisory'">Service Advisory</xsl:if>
                 <xsl:if test="g:problem = 'shuttle_bus'">Shuttle Bus</xsl:if></a><br /><br /></xsl:if>
            <xsl:if test="g:problem = 'normal_service'"></xsl:if>
         </xsl:for-each>

        <xsl:for-each select="$all_alerts[g:line_abbr = 'CCT']">
            <xsl:if test="g:problem != 'normal_service'"><xsl:value-of select="g:line"/> | <a href="/m/alert/cct.html">
                 <xsl:if test="g:problem = 'station_advisory'">Station Advisory</xsl:if>
                 <xsl:if test="g:problem = 'stop_advisory'">Stop Advisory</xsl:if>
                 <xsl:if test="g:problem = 'customer_notice'">Customer Notice</xsl:if>
                 <xsl:if test="g:problem = 'service_advisory'">Service Advisory</xsl:if>
                 <xsl:if test="g:problem = 'shuttle_bus'">Shuttle Bus</xsl:if></a><br /><br /></xsl:if>
            <xsl:if test="g:problem = 'normal_service'"></xsl:if>
         </xsl:for-each>    

        <xsl:for-each select="$all_alerts[g:line_abbr = 'NHSL']">
            <xsl:if test="g:problem != 'normal_service'"><xsl:value-of select="g:line"/> | <a href="/m/alert/nhsl.html">
                 <xsl:if test="g:problem = 'station_advisory'">Station Advisory</xsl:if>
                 <xsl:if test="g:problem = 'stop_advisory'">Stop Advisory</xsl:if>
                 <xsl:if test="g:problem = 'customer_notice'">Customer Notice</xsl:if>
                 <xsl:if test="g:problem = 'service_advisory'">Service Advisory</xsl:if>
                 <xsl:if test="g:problem = 'shuttle_bus'">Shuttle Bus</xsl:if></a><br /><br /></xsl:if>
            <xsl:if test="g:problem = 'normal_service'"></xsl:if>
         </xsl:for-each>        
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
</xsl:choose>

If there are g:line_abbr that have g:problem != 'normal service' then I want the title Subway to show as well as the following information, otherwise I want nothing to show at all even the heading of Subway.

The following doesn't hide the title 'Subway' when the for-eachs are all blank.

+1  A: 

I figured it out.

For those interested here is the part that made it work.

You have to paste this in the xsl:when statement

<xsl:when test="$all_alerts[((g:line_abbr = 'MFL') and (g:problem != 'normal_service')) or ((g:line_abbr = 'BSL') and (g:problem != 'normal_service')) or
    ((g:line_abbr = 'CCT') and (g:problem != 'normal_service')) or ((g:line_abbr = 'NHSL') and (g:problem != 'normal_service'))]">
Bry4n
A: 

Don't you think that this is better logic?

    <xsl:variable name="$test_alerts" select="$all_alerts[contains('|MFL|BSL|CCT|NHSL|',concat('|',g:line_abbr,'|')]"/>
    <xsl:if test="$test_alerts">
     <b>Subway</b><br /><br /> 
     <xsl:for-each select="$test_alerts">
        <xsl:sort select="g:line_abbr">
        <xsl:if test="g:problem != 'normal_service'">
            <xsl:value-of select="concat(g:line,' | ')"/>
            <a href="/m/alert/{translate(g:line_abbr,'MFLBSCTNH','mflbsctnh')}.html">
            <xsl:call-template name="capitalize">
                <xsl:with-param name="string" select="g:problem"/>
            <xsl:call-template>
        </xsl:if>
     </xsl:for-each>
    </xsl:if>
............................................
        <xsl:template name="capitalize">
            <xsl:param name="string"/>
            <xsl:param name="norm-string" select="concat($string,'_')"/>
              <xsl:if test="$norm-string != ''">
                 <xsl:value-of select="concat(translate(substring($norm-string,1,1),
                                                                  'qwertyuiopasdfghjklzxcvbnm',
                                                                  'QWERTYUIOPASDFGHJKLZXCVBNM'),
                                              substring(substring-before($norm-string,'_'),2),
                                              ' ')"/>
                <xsl:call-template name="capitalize">
                    <xsl:with-param name="norm-string"
                             select="substring-after($norm-string,'_')"/>
                <xsl:call-template>
              </xsl:if>
         </xsl:template>
Alejandro
It might but I don't have the time to go through and replace logistics when I don't absolutely have it. But if I do ever have the time. I will try this. Thanks!
Bry4n
@Bry4n: It's no problem. I'm adding this answer only because people seeing your code may think XSLT is verbose and not the tool for them.
Alejandro