GIDForums  

Go Back   GIDForums > Webmaster Forums > Web Design Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Apr-2003, 07:11
pcxgamer's Avatar
pcxgamer pcxgamer is offline
Senior Member
 
Join Date: Sep 2002
Location: South Carolina, USA
Posts: 1,060
pcxgamer is a jewel in the roughpcxgamer is a jewel in the roughpcxgamer is a jewel in the rough

[Tutorial] XSL Basics Pt. II


Welcome back in this article we are going to take a closer look at the example we made last time. An XSL style sheet consists of rules called templates the <xsl:template> element contains rules to apply when a specified node is matched. The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document. (i.e. match="/")

Ok here is the style sheet from the last article:
HTML Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <body> <h2>Game List</h2> <table border="1"> <tr bgcolor="#6666FF"> <th align="left">Title</th> <th align="left">Developer</th> <th align="left">Publisher</th> </tr> <xsl:for-each select="gamelist/game"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="developer"/></td> <td><xsl:value-of select="publisher"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
The style sheet is an XML document itself, so it should begin with an XML declaration: <?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet> defines the start of the style sheet.

<xsl:template> defines the start of a template. The match="/"
attribute associates the template to the root of the XML document.

The last two tags that are important are the two close tag for style sheet and template. The rest is the template itself.

At this point we have completed the XSL Template. The template will be use to tell how we want the Information displayed but right now we only have the template and no information to fill it with and that’s where the <xsl:value-of> element comes in at it extracts the value of a selected node. This element can be used to select the value of an XML element and add it to the output stream of the transformation.
HTML Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <body> <h2>Game List</h2> <table border="1"> <tr bgcolor="#6666FF"> <th align="left">Title</th> <th align="left">Developer</th> <th align="left">Publisher</th> </tr> <xsl:for-each select="gamelist/game"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="developer"/></td> <td><xsl:value-of select="publisher"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

Once you add the <xsl:value-of> element (i.e. <xsl:value-of select="title"/>) this will add content to your template.

Now you have add some content to your style sheet not alot but some. This will add one line to the content. There is one thing in the <xsl:value-of select="title"/> that I feel I should explane the forward slash selects subdirectories.

Next we will learn about the <xsl:for-each> it allows you to do looping in XSL.
HTML Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <body> <h2>Game List</h2> <table border="1"> <tr bgcolor="#6666FF"> <th align="left">Title</th> <th align="left">Developer</th> <th align="left">Publisher</th> </tr> <xsl:for-each select="gamelist/game"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="developer"/></td> <td><xsl:value-of select="publisher"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
<xsl:for-each> can be used to select every XML element of a specified node set. <xsl:for-each select="catalog/cd"> once again the forward slash selects subdirectories. We can also filter the output from an XML file by adding a criterion to the select attribute. (i.e. <xsl:for-each select="catalog/cd[developer='Relic']">)

The legal filter operators are:
= equal
!= not equal
&lt; less than
&gt; greater than

This would display only information matching your filter in this example that would be the developer being Relic.

That does it for this article. Next time we are going to look at a few more examples of XSL these will be the sort, if, and choose. The reason that I'm not going to explain then here is that these elements will not work in IE 5 because the "http://www.w3.org/TR/WD-xsl" namespace does not understand these elements.

[gid=http://www.desilva.biz/forum/viewtopic578.php]XSL Basics Pt. III[/gid]
__________________
If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization.
Last edited by pcxgamer : 21-Apr-2003 at 07:27.
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] XSL Basics Pt. III pcxgamer Web Design Forum 1 24-Apr-2003 08:04
[Tutorial] XSL Basics Pt. I pcxgamer Web Design Forum 15 22-Apr-2003 06:59
[Tutorial] MySQL Basics nniehoff MySQL / PHP Forum 15 23-Mar-2003 19:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:33.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.