Srikanth Technologies

Converting XML Elements To Attributes To Use With GridView

When we have to use an XML document with GridView through XMLDataSource, the document must have data in the form of attributes. Assume we have a document, books.xml (shown below), which contains details of books. The document contains a book element for each book with an attribute isbn and three child elements - title, author and price
<?xml version="1.0" encoding="utf-8" standalone ="yes" ?>
<books>
  <book isbn="1">
    <title>Beginning XML</title>
    <author>David Hunter</author>
    <price>500</price>
  </book>
  <book isbn="2">
    <title>ASP.NET 3.5 Unleashed</title>
    <author>Stephen Walther</author>
    <price>799</price>
  </book>
  <book isbn="3">
    <title>Programming C#</title>
    <author>Jesse Liberty</author>
    <price>450</price>
 </book>
</books>


But we cannot use this document with XMLDataSource as-it-is to display data through GridView. This is because GridView expects data to come in the form of  attributes and not elements.  So, we have to convert the data that is in the form of elements to attributes using XSL (EXtensible Stylesheet Language)  and XSLT (XSL transformations). The following .XSL file (convert.xsl) is used to convert book element, which contains one attribute and three child elements, to four attributes.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output version="1.0" method="xml" indent="yes" />
  <xsl:template match="books">
    <xsl:element name="books">
      <xsl:apply-templates/>
    </xsl:element>      
  </xsl:template>
  
  <xsl:template match="book">
    <xsl:element name="book">
      <!-- place isbn attribute first -->
      <xsl:attribute name="isbn">
        <xsl:value-of select="@isbn"/>
      </xsl:attribute>
      
      <!--  convert elements to attributes -->
      <xsl:for-each select="*">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each >
    </xsl:element>      
 </xsl:template>
</xsl:stylesheet>


XSL is used to specify how to transform XML document to HTML, XML or text.  In convert.xsl, we specify
the output to be xml. A template is used to convert book element in source to book element in output, attribute isbn to attribute isbn, and child elements of book element to attributes. XSL element  for-each is used to take one element at a time and to place it as attribute in the output. Function  name() is used to take name of the current element and  value is taken using "." with  value-of element.

If you are new to XSL, click here to learn more about XSL from a wonderful tutorial at  w3schools.com.

Now, create an asp.net page and place GridView and XMLDataSource. Specify books.xml as the XML file and convert.xsl as Transform file for XMLDataSource. Bind XMLDataSource to GridView to show data in the document as a table.

Following is the code for .aspx file.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Books</title>
</head>

<body>
   <form id="form1" runat="server">
    <h2>Books </h2>
    <asp:gridview ID="Gridview1" runat="server" 
            AutoGenerateColumns="True" 
            DataSourceID="XmlDataSource1">
    </asp:gridview>

    <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
        DataFile="books.xml" TransformFile="~/convert.xsl">     </asp:XmlDataSource>
  </form>
</body>
</html>


TransformFile attribute of XmlDataSource specifies the XSL file to be used to convert the document. Another useful attribute of XmlDataSource is XPath, which allows us to specify the condition to filter content.

Click here to learn more about XPATH.

Post Your Comment

Image

Enter the code given in the above image :
Enter Your Name :
Enter Your Email Address :
Enter Your Comment :

Comments