Displaying XML Data in Nested DataList Controls : XML DataList « XML « ASP.Net






Displaying XML Data in Nested DataList Controls

<%--
Code Revised from
       
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback)
by Thiru Thangarathinam 

# Publisher: Wrox (January 18, 2006)
# Language: English
# ISBN: 0764596772
--%>    


<%@ Page Language="C#" %>
<html>
<head>
    <title>Displaying XML Data in Nested DataList Controls</title>
</head>
<body>    
  <form runat="server">
    <h1>Bookstore: Fiction</h1>
    <asp:XmlDataSource id="MySource" DataFile="Bookstore.xml" 
      XPath="bookstore/genre[@name='Fiction']/book" runat="server"/>
    <asp:DataList id="DataList1" DataSourceId="MySource" runat="server">
      <ItemTemplate>
        <table>
          <tr>
            <td>
              <img src='<%# XPath("@ISBN") + ".jpg" %>'>
            </td>
            <td>
              <h4><%# XPath("@Title") %></h4>
              <b>ISBN:</b> <%# XPath("@ISBN") %><br>
              <b>Price:</b> <%# XPath("@Price") %><br>
            </td>
          </tr>
        </table>
          <asp:DataList id="DataList2" DataSource='<%# XPathSelect("chapter") %>' runat="server">
            <ItemTemplate>
              <br>
              <u>
                Chapter <%# XPath("@num") %>: 
                <%# XPath("@name") %>
              </u>
              <br>
              <%# XPath(".") %>
            </ItemTemplate>
          </asp:DataList>
      </ItemTemplate>
    </asp:DataList>

  </form>
</body>
</html>


<%--
<bookstore>
  <genre name="Fiction">
    <book ISBN="10-861003-324" Title="title 1" Price="19.99">
      <chapter num="1" name="Introduction">
        A
      </chapter>
      <chapter num="2" name="Body">
        B
      </chapter>
      <chapter num="3" name="Conclusion">
        C
      </chapter>
    </book>
    <book ISBN="1-861001-57-5" Title="title " Price="24.95">
      <chapter num="1" name="Introduction">
        D
      </chapter>
      <chapter num="2" name="Body">
        E
      </chapter>
      <chapter num="3" name="Conclusion">
        F
      </chapter>
    </book>   
  </genre>
  <genre name="NonFiction">
    <book ISBN="10-861003-324" Title="title 2" Price="19.99">
      <chapter num="1" name="Introduction">
        G
      </chapter>
      <chapter num="2" name="Body">
        H
      </chapter>
      <chapter num="3" name="Conclusion">
        I
      </chapter>
    </book>   
    <book ISBN="1-861001-57-6" Title="title 3" Price="27.95">
      <chapter num="1" name="Introduction">
        J
      </chapter>
      <chapter num="2" name="Body">
        K
      </chapter>
      <chapter num="3" name="Conclusion">
        L
      </chapter>
    </book>
  </genre>
</bookstore>
--%>


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

  <xsl:template match="bookstore">
    <bookstore>
      <xsl:apply-templates select="genre"/>
    </bookstore>
  </xsl:template>

  <xsl:template match="genre">
    <genre>
      <xsl:attribute name="name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      <xsl:apply-templates select="book"/>
    </genre>
  </xsl:template>

  <xsl:template match="book">
    <book>
      <xsl:attribute name="ISBN">
        <xsl:value-of select="@ISBN"/>
      </xsl:attribute>
      <xsl:element name="title">
        <xsl:value-of select="title"/>
      </xsl:element>
      <xsl:element name="price">
        <xsl:value-of select="price"/>
      </xsl:element>
      <xsl:apply-templates select="chapters/chapter" />
    </book>
  </xsl:template>

  <xsl:template match="chapter">
    <chapter>
      <xsl:attribute name="num">
        <xsl:value-of select="@num"/>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </chapter>
  </xsl:template>

</xsl:stylesheet>


--%>
           
       








Related examples in the same category