Transform XmlDataSource with XSLT : XmlDataSource « XML « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:XmlDataSource ID="sourceDVD" 
                        runat="server" 
                        DataFile="~/Data.xml"
                        TransformFile="Data.xsl"/>
     <asp:TreeView ID="TreeView1" 
                   runat="server" 
                   DataSourceID="sourceDVD"
                   AutoGenerateDataBindings="False">
            <DataBindings>
                <asp:TreeNodeBinding DataMember="Movies" Text="Movies" />
                <asp:TreeNodeBinding DataMember="DVD" TextField="Title" />
                <asp:TreeNodeBinding DataMember="Stars" TextField="Name" />
            </DataBindings>
     </asp:TreeView>
    </div>
    </form>
</body>
</html>

File: Data.xml

<?xml version="1.0"?>
<DvdList>
   <DVD ID="1" Category="Category 1">
      <Title>title 1</Title>
      <Director>directory 2</Director>
      <Price>1</Price>
      <Starring>
         <Star>star 1</Star>
         <Star>star 2</Star>
      </Starring>
   </DVD>
   <DVD ID="2" Category="Category 2">
      <Title>title 2</Title>
      <Director>directory 2</Director>
      <Price>2</Price>
      <Starring>
         <Star>star 3</Star>
         <Star>star 4</Star>
      </Starring>
   </DVD>
</DvdList>

File: Data.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml"/>
  <xsl:template match="/">
    <xsl:element name="Movies">
      <xsl:apply-templates select="//DVD" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="DVD">
    <xsl:element name="{name()}">
      <xsl:attribute name="ID">
        <xsl:value-of select="@ID"/>
      </xsl:attribute>
      <xsl:attribute name="Title">
        <xsl:value-of select="Title/text()"/>
      </xsl:attribute>
      <xsl:apply-templates select="Starring" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="Starring">
    <xsl:element name="Stars">
      <xsl:attribute name="Name">
        <xsl:value-of select="Star/text()"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>
  
</xsl:stylesheet>








25.9.XmlDataSource
25.9.1.Create XmlDataSource
25.9.2.Displaying an XML document with the Menu control.
25.9.3.XmlDataSource and XPath
25.9.4.Transform XmlDataSource with XSLT