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



Email    print   
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://sdt.bz/33737
 
Most Read Latest News Blog Resources

Add comment


Name*
Email*  
Country     


  • Comment
Loading




close
NEXT ARTICLE
Book Excerpt: The Tangled Web: A Guide to Securing Modern Web Applications
From Part III: A Glimpse of Things to Come Read More...
 
 
 
 
News on Monday
more>>
SharePoint Tech Report
more>>


   

 
 

Download Current Issue
FEBRUARY 2012 PDF ISSUE

Need Back Issues?
DOWNLOAD HERE

Want to subscribe?


 
blogs tab
Agility, mom, and apple pie
If we're to evaluate the state-of-the-art in software development, we should start with the values espoused in the Agile Manifesto.
02/07/2012 11:57 AM EST

RIM woos developers with free tablet
How do you get more apps ported to the BlackBerry PlayBook? By giving every developer a free tablet, of course!
02/04/2012 01:57 PM EST

GitHire: Use Headhunters to Find Your Perfect Programmer
Are you a hiring manager tired of scouring the job boards? Check out this new service that will find 5 people interested in your jobs.
02/03/2012 12:17 PM EST

Facebook claims hacker cred
Facebook's SEC S-1 filing form includes a short essay on the Hacker Way by Mark Zuckerberg himself.
02/02/2012 08:26 AM EST

Ryan Dahl steps down
Ryan Dahl, creator of Node.js, steps back from his position as gatekeeper for the project.
02/01/2012 04:58 PM EST

Bloomberg opens its API
Bloomberg's APIs could lead to a future standard for accessing market data.
02/01/2012 04:41 PM EST

 
Events calendar tab
2/13/2012 to 2/16/2012
Santa Clara
TechWeb

2/26/2012 to 2/29/2012
San Francisco
BZ Media

2/27/2012 to 3/2/2012
San Francisco
RSA

3/4/2012 to 3/7/2012
Las Vegas
IBM Tivoli

3/5/2012 to 3/9/2012
San Francisco
TechWeb