<pedrocorreia.net ⁄>
corner
<mySearch ⁄> <mySearch ⁄>

corner
 
corner
<mySnippets order="rand" ⁄> <mySnippets order="rand" ⁄>

corner
 
corner
<myContacts ⁄> <myContacts ⁄>

<email ⁄>


pc@pedrocorreia.net

<windows live messenger ⁄>


pedrojacorreia@hotmail.com

<myCurriculum type="pdf" ⁄>


Download
corner
 
corner
<myBlog show="last" ⁄> <myBlog show="last" ⁄>

corner
 
corner
<myNews show="rand" ⁄> <myNews show="rand" ⁄>

corner
 
corner
<myNews type="cat" ⁄> <myNews type="cat" ⁄>

corner
 
corner
<myQuote order="random" ⁄> <myQuote order="random" ⁄>

corner
 
corner
<myPhoto order="random" ⁄> <myPhoto order="random" ⁄>

<pedrocorreia.net ⁄>
corner
 
corner
<myAdSense ⁄> <myAdSense ⁄>

corner
 
corner
<myVisitorsMap ⁄> <myVisitorsMap ⁄>

corner
 
 

<Brief introduction to XSLT and Table Accessibility ⁄ >




clicks: 10321 10321 2008-06-22 2008-06-22 goto mySnippets mySnippets xml  Download  Bookmark This Bookmark This



" ...Extensible Stylesheet Language Transformations (XSLT) is an XML-based language used for the transformation of XML documents into other XML or "human-readable" documents. The original document is not changed; rather, a new document is created based on the content of an existing one ..." in Wikipedia.


Talking simpler, this is just a way to format our xml file, giving it a different layout. We use xml as our datasource, making our website dynamic and build a "normal" webpage to our viewer, without having to make use of a server-side language, such as PHP, ASP.NET, only because the browser itself will render the xslt formats, giving it a presentational layer.



The same way we can use a database to turn our webpage content dynamic, we can still do the same, but only with a XML file.


One last note before digging into code. In this snippet I'll give some focus on Table Accessibility (you can get much more useful information in this link), I won't fully support it, but I'll give some examples of thead, tfoot, tbody, headers. I hope that at least will call your attention to this.



Let's suppose we have the following XML file:

martial_arts.xml:
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <?xml-stylesheet type="text/xsl" href="format_xml.xsl"?>
  3.  
  4. <pedrocorreia.net>
  5.  
  6. <martial_arts description="This is a short Martial Arts List" title_col1="Country" title_col2="Name">
  7.  
  8. <country name="japan" description="Japan">
  9. <martial_art name="aikido" description="Aikido" />
  10. <martial_art name="kendo" description="Kendo" />
  11. <martial_art name="sumo" description="Sumo" />
  12. <martial_art name="shurikenjutsu" description="Shurikenjutsu" />
  13. </country>
  14.  
  15. <country name="china" description="China">
  16. <martial_art name="taichichuan" description="Tai Chi Chuan" />
  17. <martial_art name="shaolinkungfu" description="Shaolin kung fu" />
  18. <martial_art name="shuaijiao" description="Shuai Jiao" />
  19. <martial_art name="wingchun" description="Wing Chun" />
  20. </country>
  21.  
  22. <country name="korea" description="Korea">
  23. <martial_art name="taekwondo" description="Taekwondo" />
  24. <martial_art name="hapkido" description="Hapkido" />
  25. <martial_art name="taekyon" description="Taekyon" />
  26. <martial_art name="soobak" description="Soobak" />
  27. </country>
  28.  
  29. </martial_arts>
  30.  
  31. </pedrocorreia.net>





XML by itself is a human-readable format, this means you can easily read its data/ contents without having to get a parser, for example.


The XML above it's a regular one, I'd just point out the 2nd code line

<?xml-stylesheet type="text/xsl" href="format_xml.xsl"?>


This line of code's responsible to tell the browser that we want to "transform" our data layer.





The client will see just a normal webpage, normal html presentation, so let's give our xml a nice presentation layer by using a CSS file. Besides that, if we're going to change our xml appearance, and the output will just be an html page, it wouldn't make sense if we couldn't use CSS :)


style.css:
  1. body{
  2. font-family: Tahoma, Verdana, Arial;
  3. font-size: 62.5%;
  4. color: #000;
  5. background: #6E726C;
  6. }
  7.  
  8. div{
  9. margin: 0 auto;
  10. width: 40em;
  11. background-color: #fff;
  12. border: .1em solid #1A6B85;
  13. padding: .4em;
  14. }
  15.  
  16. table{
  17. border: solid .1em #FF5604;
  18. width: 100%;
  19. text-align: center;
  20. }
  21.  
  22. th{
  23. background-color: #1A6B85;
  24. color: #fff;
  25. font-weight: bold;
  26. font-size: 1.2em;
  27. }
  28. th.description{
  29. background-color: #FFCC66;
  30. font-weight: bold;
  31. color: #000;
  32. }
  33.  
  34. td{font-size: 1.1em;}
  35. td.cell{background-color: #C3C3C3;}
  36. td.alternate{background-color: #E1E1E1;}





So we already have our CSS defined, let's inform our parser to use it. Please note that we could also used inline styles

style.xsl, just remember this name "MartialArtsList_StyleSheet", we'll use it later ...

  1. <?xml version='1.0' encoding='utf-8' ?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:template name="MartialArtsList_StyleSheet">
  4. <link href="style.css" rel="stylesheet" type="text/css" />
  5. </xsl:template>
  6. </xsl:stylesheet>





Finally the XSLT file, you can notice that besides some xml notations and some other strange things :), this looks just like a regular xhtml file. In this snippet I'll just try to output the content to a tabular data, however it can be used to much more things than a simple table.

format_xml.xsl, please see the comments throughout the code
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output method="html"/>
  4. <xsl:include href="style.xsl"/> <!-- include xsl file that contains css file definition-->
  5. <xsl:template match="/pedrocorreia.net/martial_arts"> <!-- startup position -->
  6. <xsl:call-template name="MartialArtsList_StyleSheet"/> <!-- template name defined in style.xsl -->
  7.  
  8. <head>
  9. <!--
  10. The expression @description points to the following node:
  11. <martial_arts description="This is a short Martial Art List" title_col1="Country" title_col2="Name">
  12. This is just because we defined that our templates starts at "/pedrocorreia.net/martial_arts", it's like
  13. making a pointer to a specific position in our xml
  14.  
  15. In here we're just accessing the tag @description and get its value.
  16. Later we'll user the other 2 tags as well: "title_col1" and "title_col2",
  17. but for now let's just give the page a title.
  18. -->
  19. <title><xsl:value-of select="@description"/></title>
  20. </head>
  21. <body>
  22.  
  23. <div>
  24. <table>
  25.  
  26. <thead>
  27. <tr>
  28. <th colspan="2" id="MartialArtsList"><xsl:value-of select="@description"/></th>
  29. </tr>
  30. <tr>
  31. <th class="description" headers="MartialArtsList" id="Country">
  32. <xsl:value-of select="@title_col1" />
  33. </th>
  34. <th class="description" headers="MartialArtsList" id="Value">
  35. <xsl:value-of select="@title_col2" />
  36. </th>
  37. </tr>
  38. </thead>
  39.  
  40. <tfoot>
  41. <tr>
  42. <th colspan="2" id="MartialArtsListFooter"><xsl:value-of select="@description"/></th>
  43. </tr>
  44. </tfoot>
  45.  
  46. <tbody>
  47. <!--
  48. In XSLT, for loops we use xsl:for-each
  49. In this loop we'll iterate all the Country sub-nodes, such as:
  50. <country name="japan" description="Japan">
  51. ...
  52. -->
  53. <xsl:for-each select="country"> <!-- init foreach country -->
  54. <xsl:variable name="id" select="@name" />
  55. <tr>
  56. <th headers="MartialArtsList Country" id="Description_{$id}">
  57. <xsl:value-of select="@description"/>
  58. </th>
  59. <th headers="MartialArtsList Value" id="Name_{$id}">
  60. <xsl:value-of select="@name"/>
  61. </th>
  62. </tr>
  63.  
  64. <!--
  65. The following loop will iterate over the sub-nodes in Country, or in other words
  66. all the martial_art items inside a Country, such as:
  67. <martial_art name="aikido" description="Aikido" />
  68. ....
  69. -->
  70. <xsl:for-each select="martial_art"> <!-- init foreach martial_art -->
  71.  
  72. <!--
  73. we'll use an auxiliary variable to specify the cell class,
  74. so that we can use alternate colors.
  75.  
  76. This is basically an If ... Else
  77. -->
  78. <xsl:variable name="cell_class">
  79. <xsl:choose><!-- init condition -->
  80. <!-- condition - we use "position()-1" so we can start at 0, however, no special reason -->
  81. <xsl:when test="(position()-1) mod 2 = 0">
  82. <xsl:value-of select=" 'cell' "/> <!-- set td class -->
  83. </xsl:when>
  84. <xsl:otherwise>
  85. <xsl:value-of select=" 'alternate' "/> <!-- set td alternate class -->
  86. </xsl:otherwise>
  87. </xsl:choose><!-- end condition -->
  88. </xsl:variable>
  89.  
  90. <!--
  91. Build row
  92. note: "xls:variable" must be accessed by using curly brackets {}
  93. -->
  94. <tr>
  95. <td class="{$cell_class}" headers="MartialArtsList Country Description_{$id}">
  96. <xsl:value-of select="@description"/>
  97. </td>
  98. <td class="{$cell_class}" headers="MartialArtsList Value Name_{$id}">
  99. <xsl:value-of select="@name"/>
  100. </td>
  101. </tr>
  102.  
  103. </xsl:for-each> <!-- end foreach martial_art -->
  104. </xsl:for-each> <!-- end foreach country -->
  105. </tbody>
  106.  
  107. </table>
  108. </div>
  109. </body>
  110. </xsl:template>
  111. </xsl:stylesheet>





We won't have any html file, we can run directly (xml file) in the browser, in this snippet we only have to open "martial_arts.xml", the parser (browser) will take care of the transformations.


After opening the xml file in the browser, if you view the source code, you'll realize that source's just our xml file, no more, no less.




Here it goes a screenshot:










This is a sample of a xhtml that could be generated to reach the same thing, in other words, all this "xml transformations" would possibly generate the following code:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>This is a short Martial Arts List</title>
  6. <link rel="Stylesheet" href="style.css" />
  7. </head>
  8. <body>
  9. <div>
  10. <table>
  11.  
  12. <thead>
  13. <tr><th id="MartialArtsList" colspan="2">This is a short Martial Arts List</th></tr>
  14. <tr>
  15. <th id="Country" headers="MartialArtsList" class="description">Country</th>
  16. <th id="Value" headers="MartialArtsList" class="description">Name</th>
  17. </tr>
  18. </thead>
  19.  
  20. <tfoot>
  21. <tr><th id="MartialArtsListFooter" colspan="2">This is a short Martial Arts List</th></tr>
  22. </tfoot>
  23.  
  24. <tbody>
  25.  
  26. <tr>
  27. <th id="Description_japan" headers="MartialArtsList Country">Japan</th>
  28. <th id="Name_japan" headers="MartialArtsList Value">japan</th>
  29. </tr>
  30. <tr>
  31. <td headers="MartialArtsList Country Description_japan" class="cell">Aikido</td>
  32. <td headers="MartialArtsList Value Name_japan" class="cell">aikido</td>
  33. </tr>
  34. <tr>
  35. <td headers="MartialArtsList Country Description_japan" class="alternate">Kendo</td>
  36. <td headers="MartialArtsList Value Name_japan" class="alternate">kendo</td>
  37. </tr>
  38. <tr>
  39. <td headers="MartialArtsList Country Description_japan" class="cell">Sumo</td>
  40. <td headers="MartialArtsList Value Name_japan" class="cell">sumo</td>
  41. </tr>
  42. <tr>
  43. <td headers="MartialArtsList Country Description_japan" class="alternate">Shurikenjutsu</td>
  44. <td headers="MartialArtsList Value Name_japan" class="alternate">shurikenjutsu</td>
  45. </tr>
  46.  
  47. <tr>
  48. <th id="Description_china" headers="MartialArtsList Country">China</th>
  49. <th id="Name_china" headers="MartialArtsList Value">china</th>
  50. </tr>
  51. <tr>
  52. <td headers="MartialArtsList Country Description_china" class="cell">Tai Chi Chuan</td>
  53. <td headers="MartialArtsList Value Name_china" class="cell">taichichuan</td>
  54. </tr>
  55. <tr>
  56. <td headers="MartialArtsList Country Description_china" class="alternate">Shaolin kung fu</td>
  57. <td headers="MartialArtsList Value Name_china" class="alternate">shaolinkungfu</td>
  58. </tr>
  59. <tr>
  60. <td headers="MartialArtsList Country Description_china" class="cell">Shuai Jiao</td>
  61. <td headers="MartialArtsList Value Name_china" class="cell">shuaijiao</td>
  62. </tr>
  63. <tr>
  64. <td headers="MartialArtsList Country Description_china" class="alternate">Wing Chun</td>
  65. <td headers="MartialArtsList Value Name_china" class="alternate">wingchun</td>
  66. </tr>
  67.  
  68. <tr>
  69. <th id="Description_korea" headers="MartialArtsList Country">Korea</th>
  70. <th id="Name_korea" headers="MartialArtsList Value">korea</th>
  71. </tr>
  72. <tr>
  73. <td headers="MartialArtsList Country Description_korea" class="cell">Taekwondo</td>
  74. <td headers="MartialArtsList Value Name_korea" class="cell">taekwondo</td>
  75. </tr>
  76. <tr>
  77. <td headers="MartialArtsList Country Description_korea" class="alternate">Hapkido</td>
  78. <td headers="MartialArtsList Value Name_korea" class="alternate">hapkido</td>
  79. </tr>
  80. <tr>
  81. <td headers="MartialArtsList Country Description_korea" class="cell">Taekyon</td>
  82. <td headers="MartialArtsList Value Name_korea" class="cell">taekyon</td>
  83. </tr>
  84. <tr>
  85. <td headers="MartialArtsList Country Description_korea" class="alternate">Soobak</td>
  86. <td headers="MartialArtsList Value Name_korea" class="alternate">soobak</td>
  87. </tr>
  88.  
  89. </tbody>
  90.  
  91. </table>
  92. </div>
  93. </body>
  94. </html>




If you have any doubt or found any error, please drop me an email









clicks: 10321 10321 2008-06-22 2008-06-22 goto mySnippets mySnippets xml  Download  Bookmark This Bookmark This