After creating a blank web project in Visual Studio I added the following code to the page's class in the code behind file. This was my test page to test my transformation against a sample XML file. An XML control is added to the web form and it is used to display the XML by applying the XSLT. Prior to displaying the XML it updates the XML document to add any missing attributes to each of the data elements by comparing it to an array of expected attributes.
protected System.Web.UI.WebControls.Xml Xml1;
// XmlDocument class for loading an xml file.
private XmlDocument _doc;
// XslTransform class for loading an xslt file.
private XslTransform _transform;
// String: Path to xml file.
private string _xmlPath;
// String: Path to xslt file.
private string _xslPath;
private void Page_Load(object sender, System.EventArgs e)
{
// Get paths for XML and XSLT files
_xmlPath = Server.MapPath("data.xml");
_xslPath = Server.MapPath("trans.xslt");
// Instantiate the XmlDocument Class
_doc = new XmlDocument();
_doc.XmlResolver = null;
_doc.Load(_xmlPath);
// Array storing selected attributes to be displayed
// This array would be generated at runtime to match the web service query fields
string[] ColumnNames = new string[5];
ColumnNames[0] = "ows_Title";
ColumnNames[1] = "ows_Language";
ColumnNames[2] = "ows_Level2";
ColumnNames[3] = "ows_Level3";
ColumnNames[4] = "ows_Version";
// Retrieve all z:row elements in nodeList
XmlElement element = _doc.DocumentElement;
XmlNodeList nodeList = element.FirstChild.ChildNodes;
// for each z:row element determine if all attributes are present
// by comparing to array
// if not, then add missing attributes
foreach (XmlNode node in nodeList)
{
// get all attributes for current element
XmlAttributeCollection attrColl = node.Attributes;
// find each column in the attribute collection
foreach(string s in ColumnNames)
{
// if column is missing create attribute and add to element
if (attrColl[s] == null)
{
string prevValue = "";
XmlAttribute attr = _doc.CreateAttribute(s);
attr.Value = " ";
int columnIndex = Array.IndexOf(ColumnNames, s);
if (columnIndex == 0)
{
// insert attribute as first node
prevValue = ColumnNames[columnIndex].ToString();
node.Attributes.Prepend(attr);
}
else
{
// insert attribute in correct location
// assumes previous attribute exists
prevValue = ColumnNames[columnIndex - 1].ToString();
node.Attributes.InsertAfter(attr, attrColl[prevValue]);
}
}
}
}
// ASSERT: now have a well-formed document with no display attributes missing
// Instantiate the XslTransform Class
_transform = new XslTransform();
_transform.Load(_xslPath);
// Render the XML document
Xml1.Document = _doc;
Xml1.Transform = _transform;
}
The following sample XML is based on the returned XML from calling Sharepoint's List Web Service. This is saved as data.xml in the project.
<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema"
xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<rs:data ItemCount="50">
<z:row ows_Title="Title"
ows_Language="EN"
ows_Level2="Level2"
ows_Level3="Level3"
ows_Version="Version"
ows_Last_x0020_Modified="21;#2004-01-28 08:27:16"
ows_ID="21"
ows_owshiddenversion="2"
ows_FSObjType="21;#0"
ows_FileLeafRef="21;#Title.doc"
ows_Modified="2004-01-28 08:27:15"
ows_FileRef="21;#sites/sitename/doclibname/Title.doc"
ows_Editor="7;#Editor Name"
ows_DocIcon="doc"/>
<z:row ows_Title="Title2"
ows_Level2="Level2"
ows_Level3="Level3"
ows_Version="Version"
ows_Last_x0020_Modified="21;#2004-01-28 08:27:16"
ows_ID="21"
ows_owshiddenversion="2"
ows_FSObjType="21;#0"
ows_FileLeafRef="21;#Title2.doc"
ows_Modified="2004-01-28 08:27:15"
ows_FileRef="21;#sites/sitename/doclibname/Title2.doc"
ows_Editor="7;#Editor Name"
ows_DocIcon="doc"/>
<z:row ows_Title="Title3"
ows_Language="EN"
ows_Level2="Level2"
ows_Version="Version"
ows_Last_x0020_Modified="21;#2004-01-28 08:27:16"
ows_ID="21"
ows_owshiddenversion="2"
ows_FSObjType="21;#0"
ows_FileLeafRef="21;#Title3.doc"
ows_Modified="2004-01-28 08:27:15"
ows_FileRef="21;#sites/sitename/doclibname/Title3.doc"
ows_Editor="7;#Editor Name"
ows_DocIcon="doc"/>
</rs:data>
</listitems>
This sample shows three z:row elements. The second element has the ows_Language attribute missing and the third element has the ows_Level3 attribute missing.
So how do we transform this data into a HTML table?