lately I've been playing with xml/xslt transformation and just noticed how easy I can produce whatever contents from plain xml files, overriding my traditional time-consuming technique of parsing xml, I decided to play a lettle pit with my Advertiser module, which can be of use on websites when you want to host people's advertises. this module should do it without database at all.
First here's the structure of the sample website:
Advertiser(root)
|
|--->Admin [Amin area]
| |
| |--->Advertiser
| |
| |--->Add.aspx [add files to locations]
| |
| |--->Default.aspx [display/edit/delete files]
| |
| |--->Edit.aspx [edit files]
|
|
|---->App_Code
| |
| |--->Advertiser
| |
| |--->AddevrtiserManager.cs
|
|
|----->SiteScripts
| |
| |--->flashfix.js [used to render flash]
|
|---->uc
| |
| |--->Advertiser.ascx [user control displays the transformed file]
|
|
|---->UploadedData [+r/+w permission required]
| |
| |---->Advertiser
| |
| |--->config [Configuraion files]
| | |
| | |--->Advertiser.xml
| | |--->Advertiser.xslt
| |
| |----> [Ad. files]
|
|---->Default.aspx [Test page]
The Xml file looks like this :
<?xml version="1.0" encoding="utf-8"?>
<Advertiser>
<addvert type="Front_Content_Bottom">
<item id="4cf7f1f6-614a-4810-88f6-c73d681e9e61">
<type>FlashMovie</type>
<path>/UploadedData/Advertiser/4cf7f1f6-614a-4810-88f6-c73d681e9e61.swf</path>
<width>500</width>
<height>100</height>
</item>
</addvert>...
<xsl:param name="ExternalParameter"></xsl:param>
<xsl:param name="AdvType">AnimatedGif</xsl:param>
<xsl:template match="/">
<xsl:apply-templates select="Advertiser/addvert"/>
</xsl:template>
<xsl:template match="addvert">
<xsl:choose>
<xsl:when test="@type=$ExternalParameter">
<table cellpadding="0" cellspacing="0" border="0" id="tbl">
<xsl:for-each select="item">
<tr>
<xsl:choose>
<xsl:when test="contains(type, $AdvType)">
<td>
<a target="_blank">
<xsl:if test="string-length(normalize-space(url)) > 0">
<xsl:attribute name="href">
<xsl:value-of select="url" />
</xsl:attribute>
</xsl:if>
<img border="0">
<xsl:attribute name="src">
<xsl:value-of select="path" />
</xsl:attribute>
<xsl:attribute name="width">
<xsl:value-of select="width" />
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="height" />
</xsl:attribute>
</img>
</a>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<script type="text/javascript">
RenderFlash('<xsl:value-of select="path" />', <xsl:value-of select="width" />, <xsl:value-of select="height" />);
</script>
</td>
</xsl:otherwise>
</xsl:choose>
...............
The display page is based on a user control "Advertiser.ascx" with a property "Location" which tells the control to fetch the advertises in that belongs to this specefic location.
private Advertiser.Location _Location;
///
/// Gets or Sets Location
///
public Advertiser.Location Location
{
get { return _Location; }
set { _Location = value; }
}
protected override void Render(HtmlTextWriter writer)
{
string type = _Location.ToString();
XPathDocument xdoc = new XPathDocument(Context.Server.MapPath("~") + "/UploadedData/Advertiser/config/Advertiser.xml");
XslTransform xslt = new XslTransform();
XsltArgumentList xsltArg = new XsltArgumentList();
xsltArg.AddParam("ExternalParameter", "", type);
xslt.Load(Context.Server.MapPath("~") + "/UploadedData/Advertiser/config/Advertiser.xslt");
xslt.Transform(xdoc, xsltArg, writer);
}
While in the Admin area I decided to play dirty parsing the xml manually, to show the difference beteen the xslt style and the hard code one, although during the developement I was confronted by this annoying delima, how can I use xslt to fetch specefic location's ads and displaying them separetly in a datagrid along with eidt and delete controls.
///
/// Returns array of ads that can be used as Data source object
///
///
///
public static AdvertiseObject[] GetObjectsByLocation(Location location)
{
XmlDocument doc = GetXmlDocument();
XmlNodeList advertises = doc.GetElementsByTagName("addvert");
ArrayList arr = new ArrayList();
int counter = 0;
foreach (XmlNode advert in advertises)
{
if (advert.Attributes["type"].Value == location.ToString())
{
foreach (XmlNode item in advert.ChildNodes)
{
counter++;
AdvertiseObject adv = GetAdvObjectById(new Guid(item.Attributes["id"].Value));
arr.Add(adv);
}
}
}
AdvertiseObject[] res = new AdvertiseObject[counter];
for (int i = 0; i < counter; i++)
{
res[i] = (AdvertiseObject)arr[i];
}
return res;
}
In the user control there was no problem retrieving the ads of a location and render them directely on the control area, but I didn't manage to go arround that to feed them to a datagrid at the moment.
It is quite interesting in fact how xslt simplifies the work with xml data, that is the core of this project
Keep a running update of any changes or improvements you've made here.
| You must Sign In to use this message board. | |||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||