Solved

Applying a Complete XSL as a template

Posted on 2002-04-01
8
278 Views
Last Modified: 2011-09-20
Hello Experts,
    I am having a template which I use in around 30 separate XSLs. So if there any method whereby I can make the xsl of that template and use that xsl as a template in all other XSLs?
    I had found a method in XML Parser 3 which is xsl:include, but it is not working. Might be because I have Parser 2 and 3 both running simultaneously. So another Question is that if I want to apply a particular Parser, then how it can be done?
I want answer for both the questions.
0
Comment
Question by:nitinafre
  • 7
8 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 6911377
xsl:include must be a child of the xsl:stylesheet element.
Also, the included files path is RELATIVE to the that of the parent XSLT document.

The xsl:include works only with versions 3 and above.

Now, if you are using the namespace
http://www.w3.org/1999/XSL/Transform
and the output is correct, you are using version 3 and above and it really does not matter if you have version 2.0

With version 2.0, the only way to include another stylesheet is to append to the documentElement of the current stylesheet, the child elements of the included stylesheet.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6911773
XML Document (scripted transform) (data1.xml)
=================================
<?xml version="1.0" encoding="iso-8859-1"?>
<users>
<user id="1004">
<profile>
<name first="Michelle" middle="" last="Twain" />
<description><![CDATA[I am a CEO for a fortune 100 Company]]></description>
</profile>
</user>
<user id="1002">
<profile>
<name first="John" middle="" last="Smith" />
<description><![CDATA[I am a software enginner with ABC Corp for 14 years.]]>     </description>
</profile>
</user>
<user id="1003">
<profile>
<name first="Mark" middle="James" last="Nichols" />
<description><![CDATA[I am a sales executive]]></description>          
</profile>    
</user>
</users>

XML Document (MSIE Straight Transform) (data2.xml)
======================================
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="data.xsl" ?>
<users>
<user id="1004">
<profile>
<name first="Michelle" middle="" last="Twain" />
<description><![CDATA[I am a CEO for a fortune 100 Company]]></description>
</profile>
</user>
<user id="1002">
<profile>
<name first="John" middle="" last="Smith" />
<description><![CDATA[I am a software enginner with ABC Corp for 14 years.]]>     </description>
</profile>
</user>
<user id="1003">
<profile>
<name first="Mark" middle="James" last="Nichols" />
<description><![CDATA[I am a sales executive]]></description>          
</profile>    
</user>
</users>

Notes
=====
For simplicity sake, the XML and XSLT Documents are in the same location.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6911780
data.xsl (main XSLT document)
=============================
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="script.xsl" />
<xsl:include href="css.xsl" />
<xsl:include href="body.xsl" />
<xsl:include href="attributes.xsl" />
<xsl:output method="html" indent="yes" encoding="iso-8859-1" />
<xsl:template match="/">
<html>
<head>
<title>Using Includes</title>
<xsl:call-template name="script" />
<xsl:call-template name="css" />
</head>
<body>
<xsl:apply-templates select="users" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>

script.xsl (to handle scripts)
==============================
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="iso-8859-1" />
<xsl:template name="script">
<script language="javascript">
<xsl:comment>
<![CDATA[
var aUser = [];
function addUser(iUser,szDescription) {
      aUser["user" + iUser] = szDescription;
}
function dhtmlRoll(el) {
      el.runtimeStyle.background = event.type == "mouseover" ? "#00bfff" : el.className == "even" ? "#fdf5e6" : "e6e6fa";
}
function showInfo(el) {
      if ("object" != typeof(eDesc)) eDesc = document.getElementById("description");
      eDesc.innerHTML = aUser[el.id];
}
]]>
<xsl:for-each select="//user">addUser(<xsl:value-of select="@id" />,"<xsl:value-of select="normalize-space(profile/description)" />");
</xsl:for-each>
//</xsl:comment>
</script>
</xsl:template>
</xsl:stylesheet>


attributes.xsl (to provide attribute values)
============================================
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="iso-8859-1" />
<xsl:attribute-set name="user-table">
<xsl:attribute name="width">100%</xsl:attribute>
<xsl:attribute name="cellpadding">4</xsl:attribute>
<xsl:attribute name="cellspacing">0</xsl:attribute>
<xsl:attribute name="border">0</xsl:attribute>
<xsl:attribute name="class">user</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="header">
<xsl:attribute name="class">header</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="dhtml">
<xsl:attribute name="onmouseover">dhtmlRoll(this);</xsl:attribute>
<xsl:attribute name="onmouseout">dhtmlRoll(this);</xsl:attribute>
<xsl:attribute name="onclick">showInfo(this);</xsl:attribute>
</xsl:attribute-set>
</xsl:stylesheet>

body.xsl (to provide display of user information)
=================================================
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="iso-8859-1" />
<xsl:template match="users">
<table xsl:use-attribute-sets="user-table">
<col width="50%" />
<col width="50%" />
<tr>
      <td xsl:use-attribute-sets="header">ID</td>
      <td xsl:use-attribute-sets="header">Full Name</td>
</tr>
<xsl:apply-templates select="user">
<xsl:sort select="@id" data-type="number" order="ascending" />
</xsl:apply-templates>
</table>
<div id="description"></div>
</xsl:template>

<xsl:template match="user">
<xsl:variable name="id" select="string(@id)" />
<xsl:variable name="fullname">
      <xsl:value-of select="concat(profile/name/@last,', ',profile/name/@first)" />
      <xsl:if test="string-length(profile/name/@middle) != 0">
      <xsl:value-of select="concat(' ',substring(profile/name/@middle,1,1),'.')" />
      </xsl:if>
</xsl:variable>
<xsl:variable name="class">
      <xsl:choose>
      <xsl:when test="position() mod 2 = 0">even</xsl:when>
      <xsl:otherwise>odd</xsl:otherwise>
      </xsl:choose>
</xsl:variable>
<tr id="user{$id}" class="{$class}" xsl:use-attribute-sets="dhtml">
      <td><xsl:value-of select="$id" /></td>
      <td><xsl:value-of select="$fullname" /></td>
</tr>
</xsl:template>
</xsl:stylesheet>

css.xsl (to provide css settings)
=================================
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="iso-8859-1" />
<xsl:variable name="css">
<![CDATA[
body {margin:5px;font-family:Verdana;font-size:10pt;}
td {font-family:Verdana;font-size:10pt;}
.odd {background:#e6e6fa;}
.even {background:#fdf5e6;}
tr {cursor:hand;}
table.user {table-layout:fixed;border:1px solid #4169e1;}
td.header {font-variant:small-caps;color:#800000;letter-spacing:0.3mm;}
]]>
</xsl:variable>
<xsl:template name="css">
<style>
<xsl:value-of select="$css" disable-output-escaping="yes" />
</style>
</xsl:template>
</xsl:stylesheet>

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 23

Accepted Solution

by:
b1xml2 earned 300 total points
ID: 6911784
HTML Output
===========
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Using Includes</title>
<script language="javascript"><!--

var aUser = [];
function addUser(iUser,szDescription) {
     aUser["user" + iUser] = szDescription;
}
function dhtmlRoll(el) {
     el.runtimeStyle.background = event.type == "mouseover" ? "#00bfff" : el.className == "even" ? "#fdf5e6" : "e6e6fa";
}
function showInfo(el) {
     if ("object" != typeof(eDesc)) eDesc = document.getElementById("description");
     eDesc.innerHTML = aUser[el.id];
}

addUser(1004,"I am a CEO for a fortune 100 Company");
addUser(1002,"I am a software enginner with ABC Corp for 14 years.");
addUser(1003,"I am a sales executive");

//--></script>
<style>

body {margin:5px;font-family:Verdana;font-size:10pt;}
td {font-family:Verdana;font-size:10pt;}
.odd {background:#e6e6fa;}
.even {background:#fdf5e6;}
tr {cursor:hand;}
table.user {table-layout:fixed;border:1px solid #4169e1;}
td.header {font-variant:small-caps;color:#800000;letter-spacing:0.3mm;}

</style>
</head>
<body>
<table width="100%" cellpadding="4" cellspacing="0" border="0" class="user">
<col width="50%">
<col width="50%">
<tr>
<td class="header">ID</td>
<td class="header">Full Name</td>
</tr>
<tr onmouseover="dhtmlRoll(this);" onmouseout="dhtmlRoll(this);" onclick="showInfo(this);" id="user1002" class="odd">
<td>1002</td>
<td>Smith, John</td>
</tr>
<tr onmouseover="dhtmlRoll(this);" onmouseout="dhtmlRoll(this);" onclick="showInfo(this);" id="user1003" class="even">
<td>1003</td>
<td>Nichols, Mark J.</td>
</tr>
<tr onmouseover="dhtmlRoll(this);" onmouseout="dhtmlRoll(this);" onclick="showInfo(this);" id="user1004" class="odd">
<td>1004</td>
<td>Twain, Michelle</td>
</tr>
</table>
<div id="description"></div>
</body>
</html>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6911797
scripted transformation in ASP
==============================
<%
 Dim oXML, oXSLT
 Set oXML = Server.CreateObject("Msxml2.DOMDocument.3.0")
 Set oXSLT = Server.CreateObject("Msxml2.DOMDocument.3.0")
 oXML.async = False : oXSLT.async = False
 oXML.load Server.MapPath("xml/data1.xml")
 oXSLT.load Server.MapPath("xml/data.xsl")
 oXML.transformNodeToObject oXSLT, Response
 Set oXML = Nothing
 Set oXSLT = Nothing
%>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6911815
Notes
=====
1. To ensure that the default MSIE Parser is MSXML 3.0, you can either run xmlinst without parameters, or better still install MSXML3 SP 2 from
http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/MSDN-FILES/027/001/772/msdncompositedoc.xml&frame=true

2. The sample code, XML document and XSLT documents show the following:
1. calling a named-template.
 here we call the script and css named template.
1a. the script actually goes thru the document tree and create dynamic script in part.
1b. the css template uses the values inside a variable.

3. using attribute-sets.
 This technique centralises attributes and make it easier to change at one spot instead of throughout the XSLT documents

4. using the document nodes,
 The template that matches users and user reside in the body.xsl document and not in the data.xsl document. This shows that you can access the document tree in an included XSLT Document and that you can apply a template in the main document that calls the appropriate template in the included files.

Regards,

Brandon Driesen
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6916924
nitinafre, comments pls.
0
 

Author Comment

by:nitinafre
ID: 6919563
Brandon,
    Thanks for all the efforts and comments. Sorry for the late reply. My comp was attacked by virus bcoz.
  Well I accept your comment. But in your first comment, you had told that
"With version 2.0, the only way to include another stylesheet is to append to the documentElement of
the current stylesheet, the child elements of the included stylesheet. "
Is is not clear to me. Can you give me an example for this? Rest your answer is accepted.
Nitin
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now