Using XSLT files with the new XMLDataSource control - Raj Kaimal

Using XSLT files with the new XMLDataSource control

VS 2005 RTM (.net framework 2.0)
Target: Intermediate Developers

Consider you have an XML file like this:

 

<Employees>

  <Employee FirstName="Tom" LastName="Jones" CustomerId="1" />

  <Employee FirstName="John" LastName="Doe" CustomerId="2" />

</Employees>

 

 

You wish to display the data in a DropDownList with the DataTextField set to the FirstName and the DataValueField set to the CustomerId..

 

..and you do not want to write any code ;-)

Here is how you do it.

Drag and drop an
XmlDataSource and DropDownList control on to your page. Set their properties like so:

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Employees.xml"></asp:XmlDataSource>

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="XmlDataSource1"

DataTextField="FirstName" DataValueField="CustomerId">

</asp:DropDownList>


Run the page.

The DropDownList will get rendered like so:

<select name="DropDownList1" id="DropDownList1">

<option value="1">Tom</option>

<option value="2">John</option>

</select>

What if the XML file were like this (where the values were elements instead of attributes)?

<Employees>

  <Employee>

    <FirstName>Tom</FirstName>

    <LastName>Jones</LastName>

    <CustomerId>1</CustomerId>

  </Employee>

  <Employee>

    <FirstName>John</FirstName>

    <LastName>Doe</LastName>

    <CustomerId>2</CustomerId>

  </Employee>

</Employees>

 

Our page will fail with the following error:
DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'FirstName'.


The reason is that attributes of XML elements are promoted to properties and exposed through the XmlDataSource during databinding. In our case, FirstName is an element instead of an attribute which leads to the error.

 

So what can we do about this? Easy, we have to figure out how to transform the FirstName, CustomerId so that they become attributes of the parent node - Employee. In other words, we need to transform the second XML file so that it looks like the first XML file. This is were XSLTs come in. Explaining XLSTs is beyond the scope of this post. Please use your search engine to learn more J
XSLT Reference

The
XmlDataSource allows you to specify an XSLT file where you can define a transformation that will be applied to the DataFile you specified. 
 

Add an XSL file to your project and enter the following  (In brief, the XSLT below will transform the XML file resulting in the  FirstName, CustomerId elements becoming attributes of the Employee node)


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="Employees">

    <Employees>

      <xsl:apply-templates select="Employee"/>

    </Employees>

  </xsl:template>

  <xsl:template match="Employee">

    <Employee>

      <xsl:attribute name="FirstName">

        <xsl:value-of select="FirstName"/>

      </xsl:attribute>

      <xsl:attribute name="CustomerId">

        <xsl:value-of select="CustomerId"/>

      </xsl:attribute>

    </Employee>

  </xsl:template>

</xsl:stylesheet>

Set the XmlDataSource’s TransformFile property to point to this XSL file. Our page will look like this:

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Employees.xml" TransformFile="~/XSLTFile.xsl"></asp:XmlDataSource>

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="XmlDataSource1"

           DataTextField="FirstName" DataValueField="CustomerId">

    </asp:DropDownList>

 

The DropDownList will now get rendered like so:

<select name="DropDownList1" id="DropDownList1">

<option value="1">Tom</option>

<option value="2">John</option>

</select>

 

What if you wanted to display the FirstName, LastName in the DataTextField separated by a comma?

Easy! Change the XSL file to this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="Employees">

    <Employees>

      <xsl:apply-templates select="Employee"/>

    </Employees>

  </xsl:template>

  <xsl:template match="Employee">

    <Employee>

      <xsl:attribute name="FullName">

        <xsl:value-of select="FirstName"/>

        <xsl:text xml:space="preserve">, </xsl:text>

        <xsl:value-of select="LastName"/>

      </xsl:attribute>

      <xsl:attribute name="CustomerId">

        <xsl:value-of select="CustomerId"/>

      </xsl:attribute>

    </Employee>

  </xsl:template>

</xsl:stylesheet>


Note that we changed the attribute name to FullName.  

Note also the xml:space attribute which specifies that the space after the comma should be preserved.

Now change the page to this:

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Employees.xml" TransformFile="~/XSLTFile.xsl"></asp:XmlDataSource>

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="XmlDataSource1"

           DataTextField="FullName" DataValueField="CustomerId">

    </asp:DropDownList>


The DropDownList will get rendered like this:


<select name="DropDownList1" id="DropDownList1">

     <option value="1">Tom, Jones</option>

     <option value="2">John, Doe</option>

</select>

 

All this without writing a single line of code (if you exclude the xsl file you wrote)!

 

Time to get rid of the custom DropDownList control I wrote in v 1.1 that specifically did this!

 

 

Published Tuesday, November 22, 2005 9:02 PM by rajbk
Filed under:

Comments

# re: Using XSLT files with the new XMLDataSource control

A good one. I really like it.

Sunday, November 27, 2005 9:15 PM by DotNETKans

# re: Using XSLT files with the new XMLDataSource control

I've been trying to use this idea to allow ordering by column in an XML based gridview. I've got the xslt files, and tried to set the TransformFile property in code behind, but nothing happens.

Do you have some idea how the transform file could be switched based on events in the page?

Thanks.

Friday, December 02, 2005 1:35 PM by Paul Nations

# re: Using XSLT files with the new XMLDataSource control

Good one . Actually very useful

Friday, September 22, 2006 4:23 AM by Swapna

# re: Using XSLT files with the new XMLDataSource control

Thanks, this was really helpful.

Tuesday, September 26, 2006 6:24 AM by Mark

# re: Using XSLT files with the new XMLDataSource control

it was really interesting thank u!! but what about if u have an xml file that looks like this???: Tom Jones 1 is possible to work with xmldatasource using this kind of xml????

Thursday, November 09, 2006 12:35 PM by cmezav

# re: Using XSLT files with the new XMLDataSource control

Saturday, November 18, 2006 12:53 AM by Doug Vanderweide

# re: Using XSLT files with the new XMLDataSource control

The post is pretty useful, thanks again

Tuesday, January 30, 2007 8:16 AM by Pradeep

# re: Using XSLT files with the new XMLDataSource control

Man, I've been fighting with this for 24 hours. Not sure why this is so badly documented. Thanks a bunch!

Thursday, March 08, 2007 5:39 PM by Stephen

# re: Using XSLT files with the new XMLDataSource control

Finally! Just what I was looking for! Had no idea I had to use the transform to get my data to bind.

Tuesday, May 01, 2007 2:28 PM by Albert

# re: Using XSLT files with the new XMLDataSource control

Great to find your article !

I could've (would have !) spent hours on my issue !

Wednesday, May 09, 2007 7:16 AM by Dinch

# How To: Using XML in ASP.NET.

Everyone knows about XML, but our familiarity with actually implementing in applications may vary. I

Wednesday, June 27, 2007 11:15 PM by Dave Mackey's Computers and Programming Blog.

# re: Using XSLT files with the new XMLDataSource control

Raj - Great write up for someone new to XML or with some experience. Thanks for posting it.

Saturday, July 21, 2007 9:03 PM by synthkid

# re: Using XSLT files with the new XMLDataSource control

I'm so lucky to find your post and solved my problem which was from the first task of my new job:)

Monday, September 03, 2007 10:48 PM by Jenny

# re: Using XSLT files with the new XMLDataSource control

thanks a million for your usefull post . My problem solved .

Monday, October 15, 2007 6:55 AM by Saeedeh

# re: Using XSLT files with the new XMLDataSource control

Great! At last found this short and clear explanation. Only one question - why can't M$ do that?;)

Monday, March 31, 2008 4:40 PM by Vincas

# re: Using XSLT files with the new XMLDataSource control

amazing solution....i like it very much...

It is exactly what I was looking for.

Tuesday, April 08, 2008 7:19 AM by Brijesh

# re: Using XSLT files with the new XMLDataSource control

THANK YOU SO MUCH!  I was ripping my hair out trying to make this thing work; the minute I added a transform file, it fixed it.  Rather than getting errors,I was just getting a blank drop-down.

Wednesday, April 23, 2008 12:28 AM by Frank Lawler

# XmlDataSource &laquo; My Experiments with code

Pingback from  XmlDataSource &laquo; My Experiments with code

Saturday, June 28, 2008 3:41 PM by XmlDataSource « My Experiments with code

Leave a Comment

(required) 
(required) 
(optional)
(required)