This is intended for anyone wondering what XML can be used for...
This is just 1 of many uses, yet it exposes many posibilities of the power of XML + DOM + XSL
I just designed a new layout for my site...
It uses 1 very simple html file that functions as a shell for all the content for the page...
Note the 3 empty div tags with IDs assigned to them, we will fill these with content using script...
The style of the page is controlled by a CSS page (index.css)
I then have a vbscript (index.vbs) file that contains functions to swap out the data...
*Note: I used strings to build the Menu html tags above...
You could have, just as easily, used XSL to get the same results...
After the page loads, the LoadIndex function is called from <body onload="LoadIndex">
This loads and proccess the index.xml file which contains the menu and default page content:
After the menu is loaded, the default content is loaded with the changeContent function, which has an input for both an XML document and an XSL style sheet...
I am currently using 1 single XSL for all transformations, but if a case arises where I need to use additional XSL files, it is already set up ;-)
The XSL file I use that controls all the transformations has a catch all system, so that it can handle multiple XML files, with different types of content:
The <xsl:if> tags control which transformation to use depending on the content, as well as allow the pages to contain multiple types of content...
*Note: Since I am currently using vbScript for the scripting language, this will *probably* only work for IE browsers...
I will eventually use javascript instead, but I have been coding in BASIC for years and years, so I like to protype with it fist...
Hopefully, you might have learned a few new tricks with this Tip...
In any case, I enjoyed writing it, so thats what matters right?
It would also be interesting to see what tricks others are using, that they would like to share with the rest of us...
Happy Coding ;-)
-Josh Stribling
Visit My Site
PROGRAMMER:
Red-eyed, mumbling mammal capable of conversing with inanimate objects.
This is just 1 of many uses, yet it exposes many posibilities of the power of XML + DOM + XSL
I just designed a new layout for my site...
It uses 1 very simple html file that functions as a shell for all the content for the page...
Code:
<script language="vbscript" type="text/vbscript" src="[b]index.vbs[/b]"></script>
<html>
<head>
<link rel="stylesheet" type="text/css" href="[b]index.css[/b]" />
<title>Cube-E 101</title>
</head>
<body onload="LoadIndex">
<center>
<table class="main">
<tr><td colspan="2" align="center"><img src="img/logo1.gif" width="100%" /></td></tr>
<tr><td class="cap2" colspan="2" align="center">[b]<div id="title"/>[/b]</td></tr>
<tr>
<td valign="top">[b]<div id="menu" />[/b]</td>
<td valign="top">[b]<div id="content" />[/b]</td>
</tr>
</table>
</center>
</body>
</html>
Note the 3 empty div tags with IDs assigned to them, we will fill these with content using script...
The style of the page is controlled by a CSS page (index.css)
I then have a vbscript (index.vbs) file that contains functions to swap out the data...
Code:
Function [b]LoadIndex()[/b]
With CreateObject("MSXML.DomDocument")
.async = False
.Load "index.xml"
[COLOR=blue]MenuData = "<table class='menu'>"
For Each e In .selectNodes("//item")
xLink = e.Attributes.getNamedItem("link").Text
xTran = e.Attributes.getNamedItem("tran").Text
xName = e.Attributes.getNamedItem("name").Text
MenuData = MenuData & "<tr><td class='m'><a href='' language='javascript' onclick='changeContent(""" & xLink & """,""" & xTran & """); return false;'>" & xName & "</a></td></tr>"
Next
MenuData = MenuData & "</table>"[/color]
Set e = .selectSingleNode("//init")
xLink = e.Attributes.getNamedItem("link").Text
xTran = e.Attributes.getNamedItem("tran").Text
[b]Menu.innerhtml = MenuData[/b]
[b]changeContent xLink, xTran[/b]
End With
End Function
Function [b]changeContent(xmlFile, xslFile)[/b]
Set Dom1 = CreateObject("MSXML.DomDocument")
Set Dom2 = CreateObject("MSXML.DomDocument")
Dom1.async = False
Dom2.async = False
Dom1.Load xmlFile
Dom2.Load xslFile
[b]title.innertext = Dom1.documentElement.selectSingleNode("title").Text[/b]
[b]content.innerhtml = Dom1.transformNode(Dom2)[/b]
End Function
*Note: I used strings to build the Menu html tags above...
You could have, just as easily, used XSL to get the same results...
After the page loads, the LoadIndex function is called from <body onload="LoadIndex">
This loads and proccess the index.xml file which contains the menu and default page content:
Code:
<index>
<item link="xml/news.xml" tran="index.xsl" name="Home" />
<item link="xml/qbasic.xml" tran="index.xsl" name="QBasic" />
<item link="xml/vb.xml" tran="index.xsl" name="Visual Basic" />
<item link="xml/vcpp.xml" tran="index.xsl" name="Visual C++" />
<item link="xml/pascal.xml" tran="index.xsl" name="Pascal" />
<item link="xml/asm.xml" tran="index.xsl" name="ASM" />
<item link="xml/cs.xml" tran="index.xsl" name="C#" />
<item link="xml/tutor.xml" tran="index.xsl" name="Tutorials" />
<item link="xml/links.xml" tran="index.xsl" name="Links" />
<item link="xml/blender.xml" tran="index.xsl" name="Blender" />
<init link="xml/news.xml" tran="index.xsl" />
</index>
After the menu is loaded, the default content is loaded with the changeContent function, which has an input for both an XML document and an XSL style sheet...
I am currently using 1 single XSL for all transformations, but if a case arises where I need to use additional XSL files, it is already set up ;-)
The XSL file I use that controls all the transformations has a catch all system, so that it can handle multiple XML files, with different types of content:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:template match="/">
[b]<xsl:if test="//page">[/b]
<xsl:copy-of select="//page"/>
</xsl:if>
[b]<xsl:if test="//Site">[/b]
<table class="view">
<tr>
<th>Category</th>
<th>Site</th>
<th>Description</th>
</tr>
<xsl:for-each select="//Site">
<tr>
<td class="f"><xsl:value-of select="Category"/></td>
<td class="f">
<a href="{Link}"><xsl:value-of select="Name"/></a>
</td>
<td class="n"><xsl:value-of select="Desc"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:if>
[b]<xsl:if test="//Article">[/b]
<table class="view">
<tr>
<th>Caption</th>
<th>Author</th>
<th>Date</th>
<xsl:if test="//Text"><th>Text</th></xsl:if>
<xsl:if test="//Desc"><th>Description</th></xsl:if>
</tr>
<xsl:for-each select="//Article">
<tr>
<td class="n">
<xsl:if test="Removed"><div style="color:red"><b>REMOVED</b></div></xsl:if>
<xsl:if test="//Name"><xsl:value-of select="Name"/></xsl:if>
<xsl:if test="//Category"><xsl:value-of select="Category"/></xsl:if>
</td>
<td class="f"><xsl:value-of select="Auth"/></td>
<td class="f"><xsl:value-of select="Date"/></td>
<td class="n">
<xsl:if test="//Text"><xsl:value-of select="Text"/></xsl:if>
<xsl:if test="//Desc"><a href="#" language="javascript" onclick="changeContent('{Link}','index.xsl')"><xsl:value-of select="Desc"/></a></xsl:if>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:if>
[b]<xsl:if test="//File">[/b]
<table class="view">
<tr>
<th>Title</th>
<th>Author</th>
<th>Description</th>
<th>Screen Shot</th>
</tr>
<xsl:for-each select="//File">
<tr>
<td class="f">
<xsl:if test="Link"><a href="{Link}"><xsl:value-of select="Name"/></a></xsl:if>
<xsl:if test="not(Link)"><xsl:value-of select="Name"/></xsl:if>
</td>
<td class="f">
<xsl:if test="ALink"><a href="{ALink}"><xsl:value-of select="Auth"/></a></xsl:if>
<xsl:if test="not(ALink)"><xsl:value-of select="Auth"/></xsl:if>
</td>
<td class="n"><xsl:value-of select="Desc"/></td>
<td class="s">
<img src="{Pic}" width="160" language="javascript" alt="Click To Enlarge Image Below" onclick="scrsht.src = this.src;scrsht.width = 640" />
</td>
</tr>
</xsl:for-each>
<tr align="center">
<td colspan="4" class="cap">Screen Shot<BR /><img id="scrsht" src="img/blank.gif"/></td>
</tr>
</table>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The <xsl:if> tags control which transformation to use depending on the content, as well as allow the pages to contain multiple types of content...
*Note: Since I am currently using vbScript for the scripting language, this will *probably* only work for IE browsers...
I will eventually use javascript instead, but I have been coding in BASIC for years and years, so I like to protype with it fist...
Hopefully, you might have learned a few new tricks with this Tip...
In any case, I enjoyed writing it, so thats what matters right?
It would also be interesting to see what tricks others are using, that they would like to share with the rest of us...
Happy Coding ;-)
-Josh Stribling
Visit My Site
PROGRAMMER: