homepage Welcome to WebmasterWorld Guest from 54.237.54.83
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Server-side XML Tutorial
Xoc




msg:701694
 3:18 am on Mar 7, 2001 (gmt 0)

I use Server-side XML to help with maintenance of my web sites in a lot of ways. For example, I have a links page. I could have formed the links page using pure HTML. Originally I did. However, that made it difficult to maintain, as every time I needed to add a link, I had to specify the url twice as in:

<a href="http://www.whatever.com">http://www.whatever.com</a>

Plus I had to add all the table stuff to display it nicely formatted for each link.

Instead I moved the data to an XML file, like this:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE links SYSTEM "links.dtd">
<links>
<group title="Auctions">
<link title="eBay" href="http://www.ebay.com"/>
</group>
<group title="Books">
<link title="ABE Books" href="http://www.abebooks.com"/>
<link title="Amazon.com" href="http://www.amazon.com"/>
<link title="Barnes & Noble" href="http://www.bn.com"/>
</group>
</links>

Then by using an XSLT style sheet, I can generate the HTML necessary. The XSLT style sheet is a kind of scripting language that looks like this:


[<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Written by Greg Reddick -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" standalone="no"/>

<xsl:template match="links">
<table cellspacing="0" cellpadding="0">
<xsl:apply-templates select="group">
<xsl:sort select="attribute::title"/>
</xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="group">
<tr>
<th colspan="2" align="left" valign="bottom" height="30px">
<p class="heading"><xsl:value-of select="attribute::title"/></p>
</th>
</tr>
<xsl:apply-templates select="link">
<xsl:sort select="attribute::title"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="link">
<tr>
<td><xsl:value-of select="attribute::title"/></td>
<td>
<a href="{attribute::href}"><xsl:value-of select="attribute::href"/></a>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>

On the server you need some way of applying the XSLT style sheet to the XML file when the page gets downloaded. You could also do it statically and just generate the HTML page. I do it dynamically with an Active Server Page. You will need an XML parser. There are many available. I use the Microsoft XML parser that is available at [msdn.microsoft.com...] . I then use a bit of VBScript to apply the XSLT style sheet to the XML, generating HTML and plugging it into the page. The script I use looks like this, but is designed for a lot of flexibility and could be simpler:


<%
Function CreateHTMLFromXMLXSL(strXmlDoc, strXslDoc, astrParamValue)
'Call this function using the following syntax:
'<!--#include virtual="xslt.asp"-->
'<[percent symbol]
'=CreateHTMLFromXMLXSL( _
'Server.MapPath("filename.xml"), _
'Server.MapPath("filename.xsl"), _
'astrParamValue))
'[percent symbol]>
'Where filename.xml and filename.xsl are replaced with
'the appropriate xml and xsl filenames,
'and [percent symbol] is replaced by a %
'astrParamValue is either Empty or
'it is an array of names and values of parameters
Dim domXml
Dim domStyle
Dim nodParam
Dim iastrParamValue

set domXml = Server.CreateObject("msxml2.domdocument")
domXml.async = False
domXml.validateOnParse = False
Call domXml.setProperty("SelectionLanguage", "XPath")
If domXml.load(strXmlDoc) Then
Set domStyle = Server.CreateObject("msxml2.domdocument")
domStyle.async = False
domStyle.validateOnParse = False
If domStyle.load(strXslDoc) Then
Call domStyle.setProperty("SelectionLanguage", "XPath")
Call domStyle.setProperty("SelectionNamespaces", "xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""")
If Not IsEmpty(astrParamValue) Then
For iastrParamValue = LBound(astrParamValue) To UBound(astrParamValue) Step 2
Set nodParam = domStyle.selectSingleNode("/descendant::xsl:param[@name='" _
& astrParamValue(iastrParamValue) & "']")
nodParam.Text = astrParamValue(iastrParamValue + 1)
Next
End If
CreateHTMLFromXMLXSL = domXml.transformNode(domStyle.documentElement)
Else
CreateHTMLFromXMLXSL = XMLError(domStyle.parseError, strXSLDoc)
End If
Else
CreateHTMLFromXMLXSL = XMLError(domXml.parseError, strXMLDoc)
End If
Set domStyle = Nothing
Set domXml = Nothing
End Function

Function XMLError(parseError, strDoc)
XMLError = "Error in line " & Clng(parseError.line) & " of " & strDoc & "<br />" _
& parseError.reason
End Function
%>

The actual code I use in the page looks like this:


!--#include virtual="/_themes/xoc/xslt.asp"-->
<%=CreateHTMLFromXMLXSL(Server.MapPath("links.xml"), Server.MapPath("links.xsl"), Empty)%>

Now if you are working off of Apache, you'll have to use a different parser and scripting language, but the mechanism is the same.

You can see the final result at [xoc.net...] (ignore the stuff besides the links table--I didn't describe how that was generated!). After you get there, do a View Source in your browser. You will see that you can't tell that there was any XML involved since all the processing happened on the server.

Another advantage of this scheme, is that with a different XSLT style sheet, I can generate a CDF file that will plug all these links into the IE favorites. That style sheet looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<!-- Written by Greg Reddick -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="iso-8859-1" omit-xml-declaration="no" indent="yes" media-type="application/x-cdf"/>
<xsl:template match="/">
<channel href="http://www.xoc.net/links" base="http://www.xoc.net/link">
<title>Xoc Links</title>
<logo href="http://www.xoc.net/links/xocicon.gif" style="icon"/>
<xsl:apply-templates/>
</channel>
</xsl:template>

<xsl:template match="links">
<xsl:apply-templates>
<xsl:sort select="attribute::title"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="group">
<channel>
<title><xsl:value-of select="attribute::title"/></title>
<xsl:apply-templates>
<xsl:sort select="attribute::title"/>
</xsl:apply-templates>
</channel>
</xsl:template>

<xsl:template match="link">
<item href="{attribute::href}">
<title><xsl:value-of select="attribute::title"/></title>
</item>
</xsl:template>
</xsl:stylesheet>

The only difference between this style sheet and the previous one is the final resulting output. IE recognizes this format and generates the appropriate result.

I hope this gives you some ideas on how to make more maintainable web sites.

I apologize as all the indenting in the original files showing the structure is gone above. You can download all the original files in one zip file at [xoc.net...] .

 

grnidone




msg:701695
 5:19 pm on Mar 7, 2001 (gmt 0)

>You could also do it statically and just generate the HTML page. I do it dynamically with an Active Server Page.

Why do you do this? What is the advantage to doing it this way? (Using ASP)

>I then use a bit of VBScript to apply the XSLT style sheet to the XML, generating HTML and plugging it into the page.

I didn't believe you had to use any script to apply an XSLT style sheet to XML. Understand I am new to learn this, so I am trying to figure out if what little I have learned is flawed.

By the way, thank you for posting this. It is flagged for future studying...

-G

grnidone




msg:701696
 5:26 pm on Mar 7, 2001 (gmt 0)

>After you get there, do a View Source in your browser. You will see that you can't tell that there was any XML involved since all the processing happened on the server.

Well...mostly. There is a clue at the top:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

And it took a little while to "form" when I went to the site. Is there any way to speed this up, or would it just have to be a faster server?

-G

Xoc




msg:701697
 7:38 am on Mar 9, 2001 (gmt 0)

No, there is no automatic applying of a style sheet to XML. Even if there were, that kind of defeats the purpose of being able to apply different style sheets to the same XML.

There are two ways to speed it up. 1) You could apply the style sheet to the XML as a program and produce a HTML page on the server, in essence caching the output. There are some add-on products for ASP that will do this for you. or you could do it by hand.

2) Easier and cheaper: I am currently buffering the page (the IIS 5.0 default). That means that the entire content of the page is generated before any of it is downloaded to the web browser. I could instead either not buffer the page, or have script code that did a periodic Response.Flush to flush out the current contents to the web browser. I will probably do this in the near future.

I am out of town and am only checking in every so often so it may be a few days before I can respond.

Xoc




msg:701698
 7:42 am on Mar 9, 2001 (gmt 0)

Actually, that clue at the top isn't a clue. That is because the generated result is XHTML instead HTML. And the most formal XHTML has that declaration. See the other thread that running on XHTML for that discussion!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved