XML and XSL for True Dynamic Content

Another option you can use to create dynamic content is XML and XSL. XML is Extensible Markup Language which is similar to HTML. XSL is the stylesheet used to display the XML file.

XML Example

Using XML and XSLT, you can create dynamic content without needing to set up your web server to support dynamic pages.

Suppose you have following XML fields and values:
Field Value
FirstName John
LastName Smith
Address1 1885 California St
Address2 Suite 102
City Redwood City
State CA
Zipcode 94063
Account JohnSmith
PurchaseDate 11/07/2008

and the following external XML file:
<ExternalXml>
<SHIPPING TYPE="UPGRADE" CARRIER="FEDEX" TRACKNUMBER="1Z53X86X0325648075">
<DATE DAY="14" MONTH="05" YEAR="2000" />
</SHIPPING>
<ITEMLIST>
<ITEM NUMBER="1" QTY="1" PRODUCTDESC="Digital Camera" PRICE="100" />
<ITEM NUMBER="2" QTY="1" PRODUCTDESC="32-mb Flash" PRICE="200" />
</ITEMLIST>
</ExternalXml>
  
The resulting text message is:
Dear John,
  Thank you for your order on 09/07/2011 for
  Qty    Product Description    Item Number
-----------------------------------------------------------------
   1         Digital Camera             100
   1         32-mb Flash                200
-----------------------------------------------------------------

On Friday September 8, we shipped these items to:
John Smith
1885 California St
Redwood City, CA 94063
via Federal Express after we upgraded shipping service for you.

For your reference, use the following number to track your package: 1Z53X86X0325648075.  

Please note that tracking information may not be available immediately.

To see the latest information about your order, 
visit Your Account at http://www.worldwide.com/johnSmith

Thank you for shopping at worldwide.com.

XSL Example

Here’s an example of an XSL stylesheet that would result in the e-mail shown in the XML Example:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<pre>
Dear <xsl:value-of select="//FirstName" />,
Thank you for your order on
<xsl:value-of select="//PurchaseDate/DATE/@DAY" />
/
<xsl:value-of select="//PurchaseDate/DATE/@MONTH" />
/
<xsl:value-of select="//PurchaseDate/DATE/@YEAR" />
for Qty Product Description Item Number 
---------------------------------------------------
<xsl:call-template name="ITEMLISTING" />
---------------------------------------------------
On Friday September 8, we shipped these items to:
<xsl:value-of select="//FirstName" />
<xsl:text />
<xsl:value-of select="//LastName" />
<xsl:text />
<xsl:value-of select="//Address1" />
<xsl:text />
<xsl:value-of select="//Address2" />
<xsl:text />
<xsl:value-of select="//City" />
,
<xsl:value-of select="//State" />
<xsl:text />
<xsl:value-of select="//Zipcode" />
via
<xsl:choose>
<xsl:when test="//SHIPPING/@CARRIER = 'FEDEX'">
Federal Express after we upgraded shipping service for you.
</xsl:when>
<xsl:otherwise>UPS Ground (3-7 business days).</xsl:otherwise>
</xsl:choose>
For your reference, use the following number to track your package: 1Z53X86X0325648075.
<xsl:value-of select="//SHIPPING/@TRACKNUMBER" />

Please note that tracking information may not be available immediately. 

To see the latest information about your order, 
visit Your Account at http://www.worldwide.com/

<xsl:value-of select="//Account" />

Thank you for shopping at worldwide.com.

</pre>
</body>
</html>
</xsl:template>
<xsl:template name="ITEMLISTING">
<xsl:for-each select="//ITEMLIST/ITEM">
#
<xsl:value-of select="@QTY" />
<xsl:text />
<xsl:value-of select="@PRODUCTDESC" />
<xsl:text />
<xsl:value-of select="@PRICE" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>