xsl-list
[Top] [All Lists]

RE: [xsl] Choose conditional by tag value

2007-08-13 07:52:19
Hi Amy,

This output:
<html>
   <body>      
      <p>No events scheduled</p>
      
      <div class="xmlitem">
         <h3>
            <span class="xmlfeedtitle">
              <a
href="http://calendar.jocolibrary.org/evanced/lib/eventsignup.asp?ID=262
3%0A    "
                 >Basic PC (Personal Computers)
              </a>
            </span>
         </h3>
         <p class="xmlitemdate">09:00 AM at
            <span class="xmlfeedlocation">Corinth Library</span></p>
      </div>
   </body>
</html>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">
  
  <xsl:template match="/">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="item[title='No Events Currently Scheduled']">
    <p>No events scheduled</p>
  </xsl:template>
  
  <xsl:template match="item">
    <div class="xmlitem">
      <h3>
        <span class="xmlfeedtitle">
          <a>
            <xsl:attribute name="href">
              <xsl:value-of select="link" />
            </xsl:attribute>
            <xsl:value-of select="title" disable-output-escaping="yes"
/>
          </a>
        </span>
      </h3>
      <p class="xmlitemdate">
        <xsl:value-of select="time" /> at
      <span class="xmlfeedlocation">
          <xsl:value-of select="library" />
        </span>
      </p>
    </div>
  </xsl:template>

</xsl:stylesheet> 

By the way, you've fallen into the same trap I did when trying to learn
XSLT.  You need to shift your mindset from functional programming.

In XSLT, <xsl:apply-templates /> is the same thing as using for-each and
is the preferred method. 

And instead of using <xsl:choose><xsl:when/></xsl:choose>, create a
template match for each of the cases, as I did above.  

This will allow the xsl processor to optimize the code and will allow
you to focus on specifically what to do when a certain element is
encountered, instead of trying to code the logic to drive the
transformation.

Thanks!
Angela 

-----Original Message-----
From: Amy Drayer [mailto:amostrom(_at_)gmail(_dot_)com] 
Sent: Monday, August 13, 2007 8:31 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Choose conditional by tag value

Dear XSL list:

I am new to this, and I've tried to figure out why this isn't working
using books and websites without any success.  Please share your insight
with me!

I am styling XML that is produced by third party software.  It gives a
list of events, and if there aren't any events, it gives a message in
the <title> tag that reads No Events Currently Scheduled.  I want that
to look different than days with actual events.  So:

 <xsl:template match="/">
     <xsl:choose>
      <xsl:when  test="[title='No Events Currently Scheduled']">
            <p>No events scheduled</p>
    </xsl:when>
    <xsl:otherwise>
       <xsl:for-each select="event/item">
         <div class="xmlitem">
         <h3>
           <span class="xmlfeedtitle">
            <a>
              <xsl:attribute name="href">
                <xsl:value-of select="link" />
              </xsl:attribute>
             <xsl:value-of select="title"
disable-output-escaping="yes"></xsl:value-of></a>
           </span>
         </h3>
         <p class="xmlitemdate">
           <xsl:value-of select="time"></xsl:value-of> at
           <span class="xmlfeedlocation">
             <xsl:value-of select="library"></xsl:value-of>
           </span>
         </p>
         </div>
       </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>


And I have two XML samples to work with:
No events:
<event>
<item>
<title>No Events Currently Scheduled</title> <date/> <time/>
<description/> <location/> <library/> <link/> </item> </event>

Events:
<item>
<title>Basic PC (Personal Computers)</title> <date>Tuesday August 7th,
2007</date> <time>09:00 AM</time> <description>Want to learn how to use
personal computer? This free, one-hour basic computer training class is
an excellent place to start.
Gain experience using the mouse, keyboard and Microsoft Windows
operating system.</description> <location></location> <library>Corinth
Library</library>
<link>http://calendar.jocolibrary.org/evanced/lib/eventsignup.asp?ID=262
3
</link>
</item>
</event>

Can anyone please tell me what I am doing wrong?  I thought it was the
spaces, but it's a string... right?  Thank you for your help.

--
In peace,

Amy M. Drayer
Web Interface Designer
amostrom(_at_)gmail(_dot_)com
http://www.puzumaki.com

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>