A346 XSL include and call-template for Modularity |
Modified: |
Modular programming is a powerful tool that implements functions when there are portions of programs used repeatedly. XSL supports modular implementation of style sheets where a central style sheet file is the combination of several other style sheets.
Our goals for using modular style sheets is three-fold:
We have implemented monolithic XSL style sheets successfully so why change? Consider implementing a standard navigation layout common to most web site pages. The top and left are defined for navigation and the center is defined for page content. Something similar to that below where the numbers 111111, etc. are top navigation, the letters A, B, ... are left navigation surrounding a middle area for content.
The following lists the XSL that produces the table:
Homepage.htm
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<html>
<body>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td></td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
|
While this approach works for one page, creating other pages with the same navigation requires cloning the navigation elements and editing in the content for each page. This approach propagates the details of a standard navigation to each page, any changes may require editing each page of the Web site!
Before solving the navigation problem consider how to combine two separate XSL files. The following defines three files:
Key points:
You'll probably recognize that the xsl:template name is somewhat similar to defining a function and the xsl:call-template is similar to invoking a function.
Homepage.htm<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:include href="Content.htm" />
<xsl:template match="/">
Homepage<br/>
<xsl:call-template name="Content" />
</xsl:template>
</xsl:stylesheet>
|
Content.htm<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template name="Content">
Content of the page goes here
</xsl:template>
</xsl:stylesheet>
|
| Homepage.xml <?xml-stylesheet type="text/xsl" |
Browser output Homepage |
Modules support our implementation goals better than the monolithic solution. The following consists of four files:
The xsl:call-template generates the HTML from the named template.
Homepage.htm<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:include href="Navigation.htm" />
<xsl:include href="Content.htm" />
<xsl:template match="/">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td></td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td> </tr> </table> </xsl:template> </xsl:stylesheet> Content.htm <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template name="Content">
Content of the page goes here.
</xsl:template>
</xsl:stylesheet>
|
Navigation.htm<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template name="topNavigation">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td><a href="syllabus.htm">111111</a></td>
<td>222222</td>
<td>333333</td>
</tr>
</table>
</xsl:template>
<xsl:template name="leftNavigation">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>A</td>
</tr>
<tr>
<td>B</td>
</tr>
<tr>
<td>C</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
Homepage.xml <?xml-stylesheet type="text/xsl" Browser output
|
Note that FrontPage and Visual Studio cannot render in design view files that include other files. All can render the HTML of files that are included (e.g. Content.htm and Navigation.htm.
Exercise 1
|
Another means of combining file is called server side includes, though much less powerful than XSL. The files end with extensions of stm, shtml, etc. to signal the server to examine the file for include directives before sending to the browser. Server side includes are useful when pages can be assembled from static content located in several files.
The following example would be a file named HW1-4.stm and would display:
followed by the HTML code of the four files named in the #include.
| <H3>Homeworks 1-4</H3> <!-- #include file="HW1.htm"--> <!-- #include file="HW2.htm"--> <!-- #include file="HW3.htm"--> <!-- #include file="HW4.htm"--> |
The following server side file, Homepage.stm, contains includes for the topNavigation.htm and Content.htm files. Note that these are the much the same files used for the XSL include. There are several key differences:
Homepage.stm
<html>
<body>
<center>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td></td>
<td>
<center>
<!-- #include file="topNavigation.htm"-->
</center>
</td>
</tr>
<tr>
<td>
<!-- Left Navigation -->
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>A</td>
</tr>
<tr>
<td>B</td>
</tr>
<tr>
<td>C</td>
</tr>
</table>
</td>
<td>
<!-- #include file="Content.htm" -->
</td>
</tr>
</table>
</center>
</body>
</html>
|
topNavigation.htm<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template name="topNavigation">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td><a href="syllabus.htm">111111</a></td>
<td>222222</td>
<td>333333</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
Content.htm <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template name="Content">
Content of the page goes here.
</xsl:template>
</xsl:stylesheet>
Browser output
|
Cascading Style Sheets (CSS) function within XSL as normal in HTML. This makes sense because the XSL transformations produce the HTML before the CSS are applied. CSS will be discussed in more detail later in the course.