Showing posts with label server side transformation. Show all posts
Showing posts with label server side transformation. Show all posts

Friday, September 28, 2007

Server Side XML Transformation

Let's do it!

To transform an XML node
try something like this (the related XML is below):
<asp:xmldatasource id="MySource" datafile="~/xml/articles.xml" xpath="articles/article" runat="server">

<asp:datalist id="MyDataList" datasourceid="MySource" runat="server">
<itemtemplate>
<ul class="linklist2">
<li><a href="<%# XPath">"><%# XPath("title") %></a></li>
</ul>
</itemtemplate>
</asp:DataList>

Explanation:
XmlDataSource – defines data source
- Its XPath attribute defines the part of the doc it will draw from.
DataList – data instance drawing from the data, referring to it by id. There can be more than one.
ItemTemplate – looping template, writing data into the html where indicated by XPath controls.

The XML:
<?xml version="1.0" encoding="utf-8" ?>
<articles>
<article>
<id>1</id>
<title>1 Title Name</title>
<author>1 AuthorFirst AuthorLast</author>
<pubdate>1 Published Date</pubdate> <content>article1.html</content>
</article>
<article>
<id>2</id>
<title>2 Title Name</title>
<author>2 AuthorFirst AuthorLast</author>
<pubdate>2 Published Date</pubdate> <content>article2.html</content>
</article>
</articles>



To transform according to/using attributes
try something like this (the XML follows below):
<asp:xmldatasource id="MySource" datafile="Default.xml" xpath="Bookstore/genre/book" runat="server">

<asp:datalist id="MyDataList" datasourceid="MySource" runat="server">
<itemtemplate>

<table>
<tr>
<td>
<img alt="Cover Image" src='<%# "images/" + XPath("@ISBN") + ".gif" %>' >
</td>
<td>
<h4><%# XPath("@Title") %></h4>
<b>ISBN:</b> <%# XPath("@ISBN") %><br />
<b>Price:</b> <%# XPath("@Price") %>

</u>
<br />
<%# XPath(".") %>
</itemtemplate>
</asp:DataList>

</itemtemplate>
</asp:DataList>


The related XML:
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<genre name="Business">
<book isbn="BU1032" title="The Busy Executive's Database Guide" price="19.99">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
<book isbn="BU2075" title="You Can Combat Computer Stress!" price="2.99">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
<book isbn="BU7832" title="Straight Talk About Computers" price="19.99">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
</genre>
</bookstore>
- Yeah, baby
=)



========================================================================
another way - for repeating controls, using the XML control:
========================================================================

The asp.net control:

<asp:Xml ID="xmlArticle" runat="server" DocumentSource="~/xml/articles.xml" TransformSource="~/xsl/articles.xsl"></asp:Xml>

the XML:
<?xml version="1.0" encoding="utf-8" ?>
<articles>
<article>
<id>1</id>
<title>1 Title Name</title>
<author>1 AuthorFirst AuthorLast</author>
<pubdate>1 Published Date</pubdate> <content>article1.html</content>
</article>
<article>
<id>2</id>
<title>2 Title Name</title>
<author>2 AuthorFirst AuthorLast</author>
<pubdate>2 Published Date</pubdate> <content>article2.html</content>
</article></articles>

The XSL:
<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform%22&gt;
<xsl:output method="xml"></xsl:output>
<xsl:template match="/articles">
<xsl:for-each select="./article">
<div>
<xsl:attribute name="id">article-<xsl:value-of select="id">
</xsl:value-of>
</xsl:attribute>
<h2>
<xsl:value-of select="title"/>
</h2>
<h3>
<xsl:value-of select="pubdate"/>
</h3>
<p>
<xsl:value-of select="content"/>
</p>
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>