Solved

Applying a Complete XSL as a template

Posted on 2002-04-01
8
275 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 23

Accepted Solution

by:
b1xml2 earned 300 total points
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 23

Expert Comment

by:b1xml2
Comment Utility
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
Comment Utility
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
Comment Utility
nitinafre, comments pls.
0
 

Author Comment

by:nitinafre
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

14 Experts available now in Live!

Get 1:1 Help Now