Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

Next Webcast - Simple SQL Server Reporting - Click Here to Register
 

Using XML and XSL in SharePoint


By:   |   Read Comments   |   Related Tips: > Sharepoint

Problem

The standard approach for data storage in SharePoint is lists and libraries. However, there are a few scenarios where you need to deal with XML data. Consider a scenario where you need to integrate a flash component in SharePoint which has dynamic XML data. Or consider another case where you need to manipulate XML data returned by a web service.

XML is more suitable for hierarchical data. So in some cases you would want list data to be converted into XML first, before doing any manipulation. Now the next question is - how would you render the output as desired HTML from an XML data source?

Solution

XSLT (eXtensible Stylesheet Language Transformations) comes to the rescue for XML data presentation. XSLT is the recommended style sheet language of XML. XSLT can be used to transform XML into HTML, before it is displayed by a browser. There are various mechanisms in SharePoint which can be used to display XML data using XSL Transformation. We will discuss various approaches(Data View, XML web part and Custom Code) as follows.

For the purposes of this article, let us consider the following XML data. To download the XML click here

edgewood solutions

Data View / Form Web Parts (DVWP): DVWPs are able to retrieve data from various data sources in the form of XML, even if the data itself in its original form is not XML. The eXtensible Stylesheet Language Transformations (XSLT) can be used to change the appearance of returned data. These web parts can only be created using SharePoint Designer (SPD). Now that SharePoint Designer 2007 is free, this opens the door to a lot more people being able to create these web parts, without any need of server access or deployment. Follow these steps to create a DVWP with XML as data source.

- Launch SharePoint Designer and open the site. Open the page in SPD where you want to add the DVWP.

- Select the web part zone, go to Data View toolbar and click on "Insert Data View"

instant data view

- Before you can use the XML as a data source, you need to upload it somewhere in the site. I prefer to upload it in a separate folder in Style library. Once done, go back to SPD and expand the XML files under the Data Source Library. Click on 'Add an XML file' if you don't see your XML listed there.

xml file

- Browse to the location where you uploaded your xml and click Open. Click OK on the next window.

sample

- Click on the down arrow beside the XML file and click on "Show Data".

show data

- Select the field that you want to show and click on Insert as Multiple Item View. In this case, we will select title, audience and issue

current data source

- Save the page and see it in the browser or design window.

You will see the data in crude form.

super admin

- This is not the end result we want. Let us make this look tidy and meaningful by creating a custom XSL. You can modify XSL using SPD in code view. I will explain the XSL creation tricks in detail in my next article. For this example, you can download the custom XSL here. After you apply the XSL, you will see the output as follows

sharepoint

Save the page. You can export this web part and use it in other places with the same XML data source.

XML Web Part: SharePoint provides an out-of-the-box Webpart which essentially takes two inputs. XML and XSL file paths. You can learn more about the XML web part here. Download the XSL for this example here.

- Go to a SharePoint page and add an XML web part

outlook web access

- Upload the XSL somewhere (style library is the most logical place) on the site. Reference the links to XML and XSL in the web part properties and XML web part will do the rest.

xml web part
sharepoint

Transforming XML using XSL and .NET: There are scenarios where you need dynamic variables to interact with web parts whose values change depending on the Page metadata and other environmental parameters. For example, let's say there is a dynamic variable "Type" which needs to be manipulated and displayed in the web part. Also, there are some cases where XML data is dynamic (although the schema or structure is same). In those cases, you will create .NET custom a web part to read the XML file and dynamic variables, and apply XSLT through code.

After you get the data as XML document in your code, you can use the following code to transform your XML. If you are using this in many web parts, I would recommend that you add this to a utility class and call the function wherever required.

//These 2 namespaces are required for this functionality
using System.Xml;
using System.Xml.Xsl

//This will hold XmlDocument containing xml data
XmlDocument doc = new XmlDocument();

//This will get the XSL path stored on the site
string xslPath = "/sample.xsl";

//This will output HTML to the rendering control
string outputHTML = String.Empty;

//Create an XsltArgumentList. Will add run time parameters
XsltArgumentList xslArg = new XsltArgumentList(); 

//Example of a run time parameter
string Type = String.Empty;
//Run time Parameter is being added to XsltArgument List (multiple parameters can be added). Same parameters need to be declared in XSL.
xslArg.AddParam("Type", "", Type);

//Call XML tranformation : Pass "XmlDocument", "XslPath" and "XsltArgumentList"
outputHTML = ConvertXML(doc, xslPath, xslArg);

//Following function will be called passing doc, xslPath and xslArg and return HTML string
public string ConvertXML(XmlDocument InputXMLDocument, string XSLTFilePath, XsltArgumentList XSLTArgs
{ 
InputXMLDocument = doc;
StringWriter sw = new System.IO.StringWriter();
XslCompiledTransform xslTrans = new XslCompiledTransform();

SPSite currentsite = null;
SPWeb currentweb = null;
currentsite = SPContext.Current.Site;
using (currentweb = currentsite.OpenWeb())
{

//Loads the xsl from path to transform
SPFile file = currentweb.GetFile(XSLTFilePath);
XmlReader r = XmlReader.Create(file.OpenBinaryStream());
xslTrans.Load(r);

//XML transformation using XSL and passing 
xslTrans.Transform(InputXMLDocument.CreateNavigator(), XSLTArgs, sw); 
}
return sw.ToString(); 
}

After, you get well versed with these approaches; you can then make an intelligent decision on whether to use DVWP, XML web part or .NET custom web part. As a rule of the thumb, try to approach a solution using DVWP or XML web part. Since, this does not require any deployment; it is always easy and quick to make future updates. However, DVWP and XML web part have a few limitations. It cannot handle dynamic XML and variables. Develop .NET custom web parts in those scenarios.

Next Steps


Last Update:


next webcast button


next tip button



About the author





Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Email me updates 


Signup for our newsletter
 I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.



    



Learn more about SQL Server tools