Most Read Latest News Blog Resources
Digg!  Digg
Reddit  Reddit


            iphoneapp GET THE APP!

Share Pointers: Conditional Logic in XSL Using the XML Web Part




September 2, 2009 —  (Page 1 of 3)
One common requirement when working with data in SharePoint is to use conditional logic to style some data differently based on various conditions. This could be as simple as only showing certain data elements when their price is greater than a specific level, or you might need to do some more complex logic by coloring certain levels of prices differently.

All of this can be accomplished in many SharePoint Web parts by using XSL (Extensible Stylesheet Language). In this article, we will look at using the XML Web Part to read XML book data and use conditional logic to slice and color the items.

To start the example, create a Web part page in SharePoint and add an XML Web part. From there, we will edit the Web part and add some XML data. Click Edit > Modify Shared Web Part, and then in the Web part editor menu on the right side of the screen, click XML Editor… Enter the following XML data:
<books>
    <book>
        <title>Professional SharePoint 2007 Design</title>
        <price>31.49</price>
    </book>
    <book>
        <title>War and Peace</title>
        <price>3.33</price>
    </book>    
    <book>
        <title>Professional Microsoft Search: SharePoint 2007 and Search Server 2008</title>
        <price>43.21</price>
    </book>
</books>


This data describes three books of various price levels. Next we can add some generic XSL to the Web part to begin styling it. Click XSL Editor… and enter
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="html" />

    <xsl:template match="books/book">
        <xsl:value-of select="title"/><br/>
        $<xsl:value-of select="price"/><br/>
        <br/>
    </xsl:template>
</xsl:stylesheet>


This XSL will loop over all of the books and show each title and price with a dollar sign in front, like this:
Professional SharePoint 2007 Design
$31.49

War and Peace
$3.33

Professional Microsoft Search: SharePoint 2007 and Search Server 2008
$43.21
Let’s pretend we need to only show books that were over $5. How would we do that? Since this is a simple condition, we can use <xsl:if>, which tests to see if a condition is true, and if it is, anything included inside the element will be shown. If you replace the original XSL with the following code, you can see it in action:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="html" />


Pages 1 2 3 


Share this link: http://www.sdtimes.com/link/33737
 

Add comment


Name*
Email*  
Country     


  • Comment
  • Preview
Loading



 
 
 
 
News on Monday
more>>
SharePoint Tech Report
more>>


   

 
 
Download Current Issue
ISSUE 8/1/2010 PDF

Need Back Issues?
DOWNLOAD HERE

Receive the print Edition?


 
blogs tab
Like Ruby n' Rails
Programming languages and Web frameworks go together like peas n carrots. Or Ruby n' Rails.
07/30/2010 04:36 PM EST

Adobe buys a Web-based IDE
Adobe looks to buy a rapid and agile Web development environment.
07/28/2010 03:49 PM EST

OpenStack opens up
OpenStack looks to be an intriguing new idea for running clouds.
07/28/2010 01:56 PM EST

 

Events calendar tab
8/1/2010 to 8/5/2010
Boston
SHARE

8/2/2010 to 8/6/2010
Redmond, Wash.
1105 Media

8/9/2010 to 8/13/2010
Orlando
Agile Alliance

8/23/2010 to 8/26/2010
San Jose
S&S Media

8/30/2010 to 9/2/2010
San Francisco
VMware