XSLT Context

[XSLT xslref=...(or xslsource=...)&xmlsource=][/XSLT]


The [XSLT] Context allows the WebDNA programmer to 'apply' an XSL style sheet to an XML document and thus 'transform' the XML data into any format the programmer desires (usually HTML).


Optional Tag Parameters:

xslref - named reference to a previously compiled block of XSL source.

xslsource - URL path to an external XSL document. This is an alternative to using the xslref parameter. Meaning that it is possible to use the [XSLT] context without having used the [XSL] context to pre-compile XSL source code. However, if you plan to use the same XSL code several times throughout the template, then it would be more efficient to compile the XSL source code once, using [XSL], then just use the XSL object reference when needed.

xmlsource - URL reference to an external XML document on which to apply the XSL transformation. If this parameter is supplied, then any in-line XML code that exists between the [XSLT] tags will be ignored.


Example - Select all the 'CD' nodes in the example1.xml XML document and transform the results into an HTML table, with the 'text' data of each CD node tree displayed in the table rows.

Here is the code...

[!] First use the [XSL] Context to parse the style sheet [/!]
[xsl var=xsl_var1]
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
[/xsl]

[xslt xslref=xsl_var1][include file=example1.xml][/xslt]


Results...

 

My CD Collection

Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tylor
Greatest Hits Dolly Parton
Still got the blues Gary More
Eros Eros Ramazzotti
One night only Bee Gees
Sylvias Mother Dr.Hook
Maggie May Rod Stewart
Romanza Andrea Bocelli
When a man loves a woman Percy Sledge
Black angel Savage Rose
1999 Grammy Nominees Many
For the good times Kenny Rogers
Big Willie style Will Smith
Tupelo Honey Van Morrison
Soulsville Jorn Hoel
The very best of Cat Stevens
Stop Sam Brown
Bridge of Spies T`Pau
Private Dancer Tina Turner
Midt om natten Kim Larsen
Pavarotti Gala Concert Luciano Pavarotti
The dock of the bay Otis Redding
Picture book Simply Red
Red The Communards
Unchain my heart Joe Cocker

 

This is a pretty simple table, but it should give you an idea of what you can do with XSL(T).

Here is the same example, but without using the XSL context to pre-compile the in-line XSL code. Instead, a URL reference is made to an external xsl file, which happens to match the XSL code block above. It also uses the 'xmlsource' parameter to reference an external xml file to transform.

Here is the source...


[!] build the URL path to this lab folder [/!]
[text]host=[listmimeheaders name=HOST&exact=F][value][/listmimeheaders][/text]
[text]path=[url][listpath pathonly=T&path=[thisurl]][name]/[/listpath][/url][/text]

[xslt xslsource=http://[host]/[path]example1.xsl&xmlsource=http://[host]/[path]example1.xml][/xslt]

 

Results (should be the same as above)...

 

My CD Collection

Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tylor
Greatest Hits Dolly Parton
Still got the blues Gary More
Eros Eros Ramazzotti
One night only Bee Gees
Sylvias Mother Dr.Hook
Maggie May Rod Stewart
Romanza Andrea Bocelli
When a man loves a woman Percy Sledge
Black angel Savage Rose
1999 Grammy Nominees Many
For the good times Kenny Rogers
Big Willie style Will Smith
Tupelo Honey Van Morrison
Soulsville Jorn Hoel
The very best of Cat Stevens
Stop Sam Brown
Bridge of Spies T`Pau
Private Dancer Tina Turner
Midt om natten Kim Larsen
Pavarotti Gala Concert Luciano Pavarotti
The dock of the bay Otis Redding
Picture book Simply Red
Red The Communards
Unchain my heart Joe Cocker

 

This xsl example will display all the CD 'Titles' and 'Artists' and add a pink background-color to the artist column WHEN the price of the cd is higher than 10.

Here is the code...


[xsl var=xsl_var1]
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<xsl:choose>
<xsl:when test="PRICE&gt;'10'">
<td bgcolor="#ff00ff">
<xsl:value-of select="ARTIST"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="ARTIST"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
[/xsl]

[xslt xslref=xsl_var1][include file=example1.xml][/xslt]

 

Results...


My CD Collection

Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tylor
Greatest Hits Dolly Parton
Still got the blues Gary More
Eros Eros Ramazzotti
One night only Bee Gees
Sylvias Mother Dr.Hook
Maggie May Rod Stewart
Romanza Andrea Bocelli
When a man loves a woman Percy Sledge
Black angel Savage Rose
1999 Grammy Nominees Many
For the good times Kenny Rogers
Big Willie style Will Smith
Tupelo Honey Van Morrison
Soulsville Jorn Hoel
The very best of Cat Stevens
Stop Sam Brown
Bridge of Spies T`Pau
Private Dancer Tina Turner
Midt om natten Kim Larsen
Pavarotti Gala Concert Luciano Pavarotti
The dock of the bay Otis Redding
Picture book Simply Red
Red The Communards
Unchain my heart Joe Cocker

 


This example will display all the CD 'Titles' and 'Artists' sorting them by artist.

Here is the code...


[xsl var=xsl_var1]
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<xsl:sort select="ARTIST"/>
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
[/xsl]

[xslt xslref=xsl_var1][include file=example1.xml][/xslt]

 

Results...


My CD Collection

Title Artist
Romanza Andrea Bocelli
One night only Bee Gees
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tylor
The very best of Cat Stevens
Greatest Hits Dolly Parton
Sylvias Mother Dr.Hook
Eros Eros Ramazzotti
Still got the blues Gary More
Unchain my heart Joe Cocker
Soulsville Jorn Hoel
For the good times Kenny Rogers
Midt om natten Kim Larsen
Pavarotti Gala Concert Luciano Pavarotti
1999 Grammy Nominees Many
The dock of the bay Otis Redding
When a man loves a woman Percy Sledge
Maggie May Rod Stewart
Stop Sam Brown
Black angel Savage Rose
Picture book Simply Red
Bridge of Spies T`Pau
Red The Communards
Private Dancer Tina Turner
Tupelo Honey Van Morrison
Big Willie style Will Smith

 

Append XML data to a database

In this example we use XSLT to generate WebDNA [replace] code to replace/append XML data to a WebDNA database. It is a little tricky embedding the WebDNA into the XSL code, but it can be done.

Here is the code...


[xsl var=xsl_var1]


[!]
[/!]%5Breplace db=music.db[url]&[/url]eqTITLEdata=[url]&[/url]append=T%5D[!]
[/!]TITLE=[url]&[/url][!]
[/!]ARTIST=%5B/replace%5D

 

[/xsl]

[text]result=[unurl][xslt xslref=xsl_var1][include file=example1.xml][/xslt][/unurl][/text]

 

The XSLT operation produced the following WebDNA code...


[result]

We then use the interpret context to 'parse' WebDNA code. And use the search context to confirm that the entries where added.


[!]clear out the database entries[/!]
[delete db=music.db&neTITLEdata=[blank]]

[interpret]
[result]
[/interpret]

[search db=music.db&neTITLEdata=[blank]]


Found [numfound] CD entries in the music database.


[founditems]
[TITLE] - [ARTIST]

[/founditems]
[/search]

[closedatabase db=music.db]

Results...


The XSLT operation produced the following WebDNA code...

[replace db=music.db&eqTITLEdata=Empire Burlesque&append=T]TITLE=Empire Burlesque&ARTIST=Bob Dylan[/replace]
[replace db=music.db&eqTITLEdata=Hide your heart&append=T]TITLE=Hide your heart&ARTIST=Bonnie Tylor[/replace]
[replace db=music.db&eqTITLEdata=Greatest Hits&append=T]TITLE=Greatest Hits&ARTIST=Dolly Parton[/replace]
[replace db=music.db&eqTITLEdata=Still got the blues&append=T]TITLE=Still got the blues&ARTIST=Gary More[/replace]
[replace db=music.db&eqTITLEdata=Eros&append=T]TITLE=Eros&ARTIST=Eros Ramazzotti[/replace]
[replace db=music.db&eqTITLEdata=One night only&append=T]TITLE=One night only&ARTIST=Bee Gees[/replace]
[replace db=music.db&eqTITLEdata=Sylvias Mother&append=T]TITLE=Sylvias Mother&ARTIST=Dr.Hook[/replace]
[replace db=music.db&eqTITLEdata=Maggie May&append=T]TITLE=Maggie May&ARTIST=Rod Stewart[/replace]
[replace db=music.db&eqTITLEdata=Romanza&append=T]TITLE=Romanza&ARTIST=Andrea Bocelli[/replace]
[replace db=music.db&eqTITLEdata=When a man loves a woman&append=T]TITLE=When a man loves a woman&ARTIST=Percy Sledge[/replace]
[replace db=music.db&eqTITLEdata=Black angel&append=T]TITLE=Black angel&ARTIST=Savage Rose[/replace]
[replace db=music.db&eqTITLEdata=1999 Grammy Nominees&append=T]TITLE=1999 Grammy Nominees&ARTIST=Many[/replace]
[replace db=music.db&eqTITLEdata=For the good times&append=T]TITLE=For the good times&ARTIST=Kenny Rogers[/replace]
[replace db=music.db&eqTITLEdata=Big Willie style&append=T]TITLE=Big Willie style&ARTIST=Will Smith[/replace]
[replace db=music.db&eqTITLEdata=Tupelo Honey&append=T]TITLE=Tupelo Honey&ARTIST=Van Morrison[/replace]
[replace db=music.db&eqTITLEdata=Soulsville&append=T]TITLE=Soulsville&ARTIST=Jorn Hoel[/replace]
[replace db=music.db&eqTITLEdata=The very best of&append=T]TITLE=The very best of&ARTIST=Cat Stevens[/replace]
[replace db=music.db&eqTITLEdata=Stop&append=T]TITLE=Stop&ARTIST=Sam Brown[/replace]
[replace db=music.db&eqTITLEdata=Bridge of Spies&append=T]TITLE=Bridge of Spies&ARTIST=T`Pau[/replace]
[replace db=music.db&eqTITLEdata=Private Dancer&append=T]TITLE=Private Dancer&ARTIST=Tina Turner[/replace]
[replace db=music.db&eqTITLEdata=Midt om natten&append=T]TITLE=Midt om natten&ARTIST=Kim Larsen[/replace]
[replace db=music.db&eqTITLEdata=Pavarotti Gala Concert&append=T]TITLE=Pavarotti Gala Concert&ARTIST=Luciano Pavarotti[/replace]
[replace db=music.db&eqTITLEdata=The dock of the bay&append=T]TITLE=The dock of the bay&ARTIST=Otis Redding[/replace]
[replace db=music.db&eqTITLEdata=Picture book&append=T]TITLE=Picture book&ARTIST=Simply Red[/replace]
[replace db=music.db&eqTITLEdata=Red&append=T]TITLE=Red&ARTIST=The Communards[/replace]
[replace db=music.db&eqTITLEdata=Unchain my heart&append=T]TITLE=Unchain my heart&ARTIST=Joe Cocker[/replace]

We then use the interpret context to 'parse' WebDNA code. And use the search context to confirm that the entries where added.

Found 26 CD entries in the music database.

Bridge of Spies - T`Pau
The dock of the bay - Otis Redding
Black angel - Savage Rose
The very best of - Cat Stevens
Unchain my heart - Joe Cocker
Romanza - Andrea Bocelli
Midt om natten - Kim Larsen
Eros - Eros Ramazzotti
Red - The Communards
For the good times - Kenny Rogers
Soulsville - Jorn Hoel
Big Willie style - Will Smith
Maggie May - Rod Stewart
Stop - Sam Brown
Still got the blues - Gary More
Private Dancer - Tina Turner
When a man loves a woman - Percy Sledge
Pavarotti Gala Concert - Luciano Pavarotti
Hide your heart - Bonnie Tylor
Picture book - Simply Red
1999 Grammy Nominees - Many
One night only - Bee Gees
Empire Burlesque - Bob Dylan
Greatest Hits - Dolly Parton
Sylvias Mother - Dr.Hook
Tupelo Honey - Van Morrison

User Contributed Notes
XSLT  

Copyright © 2008 WebDNA Software Corporation, Inc.