[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Choose conditional by tag value

Subject: RE: Choose conditional by tag value
From: "Angela Williams" <Angela.Williams@xxxxxxxxxxxxxxxxxx>
Date: Mon, 13 Aug 2007 09:51:55 -0500
RE:  Choose conditional by tag value
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@xxxxxxxxx]
Sent: Monday, August 13, 2007 8:31 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  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@xxxxxxxxx
http://www.puzumaki.com

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.