473,503 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically reusing XSLT templates across XSL files

Hi all,

I've created XML documents that are described with a schema. I'm using those
documents to create web pages.

All my web pages contain a fixed header and a variable document part. The
header is the same in each page and is described in an XML document,
"Head.xml". The document part, which is variable in content, is described in
other XML files (e.g. "Document.xml", "Product.xml", "Register.xml").

Following the liquid design way, a web page is always built from "Head.XML"
and (only) one XML document. A page is always made of a succession of DIV
elements that are positioned using CSS. A DIV corresponds to a piece of
information in the schema.

I defined the base structure of a document in a schema and I wanted that
structure to be extensible. Hence I've extended the definition of a document
to fit other requirements (e.g. form submission documents - "Register.xml",
product sheet - "Product.xml").

For instance, static information contain a title and a body text. The title
and the text form the base document in "Document.xml". A product document,
"Product.xml", extends the base document definition to add a list of
packages. Packages are also rendered in HTML as a DIV below the body text.

The XSL templates required to output a page HTML, HEAD, BODY and document
header are placed in an XSL file, "Head.xsl". The XSL templates to output a
document are placed in another XSL file, "Document.xsl". The templates to
output a product are placed in yet another XSL file, "Product.xsl".

How can I reuse those templates given that the template that outputs the
structure of the web page must be called first?

A concrete case: to output the web page for a document, I need to call some
templates in "Head.xsl", plus some other templates in "Document.xsl" that
should be called in-between. If I want to output the page for a product I
have to call the same templates from "Head.xsl", that should in turn call
some templates from "Document.xsl" (to render title and text) and finally
call some templates from "Product.xsl" (to render packaging below the text).

Such an order in calls is required because the template that renders the
structure of a page also renders the HTML and BODY elements. So I have to
call templates dynamically from within "Head.xsl".

Is there a smart(er) way to achieve this?

Thanks for any hint/suggestion.

Vince C.
Jul 20 '05 #1
1 2547
Thanks a lot, Colin.

See my comments below.

"Colin Mackenzie" <co***@elecmc.com> a écrit dans le message de news:
bd**********@news8.svr.pol.co.uk...
Hi,

I am not sure I fully understand your requirement but here goes,

why not do as follws

1/ transform the XML using Document.xsl/products.xsl etc as required
depending on the type of page
2/ ensure that BOTH document and product XSL include or import head.xsl (see
xsl:include)
3/ ensure that the match for the root or document element of the XML exists
only in the head.xsl thus
a) when the root/document element is matched the <html<body> elments are
output
b) inside the body tag have an apply-tempaltes (to allow the contents to be
processed by a matched template)
c) when the content is matched (by the appropriate match in the appropraite
stylesheet) the content is output

Does this match your requirement?


Perfectly.

In fact, I took a deeper look to <xsl:import>. As you pointed out, it does
everything I want provided <xsl:apply-templates> is used whenever applicable. I
never used XSL imports before so I made a test with a couple of XML and XSL
files.

I created an XML file with 2 elements: "a" and "b". I then created 2 XSL files:
"A.xsl" and "B.xsl". The former contained a template for element "a" and the
main HTML template, the latter contained only a template for element "b" plus an
import of template "B.xsl". I transformed the XML using "B.xsl" and all
templates were called.

-- Test.xml --
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="B.xsl"?>
<root>
<a>First element</a>
<b>Second element</b>
</root>

-- A.xsl --
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="xsl fo"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="a">
<p>This is element <b>A</b>: <i>"<xsl:value-of select="text()"/>"</i></p>
</xsl:template>

<xsl:template match="/">
<html>
<head>
<title>Dynamic Templates Test Page</title>
</head>
<body>
<xsl:apply-templates select="/root"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

-- B.xsl --
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="xsl fo"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:import href="A.xsl"/>
<xsl:template match="b">
<p>This is element <b>B</b>: <i>"<xsl:value-of select="text()"/>"</i></p>
</xsl:template>
</xsl:stylesheet>

-- Output --
This is element A: "First element"

This is element B: "Second element"
----

Ok, I've clearly discovered (Smirnoff Ice?) the <xsl:import> mechanism... But I
now realize its power: it's as flexible as Object Oriented Programming when you
override classes and create polymorphic containers.

Importing templates is just like deriving classes in C++: you can reuse
templates on documents that you didn't design just by importing the existing
ones into new stylesheets. All you have to do is provide templates for new
elements in the instance file or redefine existing templates.

This gives the same flexibility as deriving classes and overriding member
functions. As you also pointed out the structure of stylesheets and documents is
also important.

To bring a complete solution to my puzzle, I can put a reference to "Head.xml"
in "Document.xsl" using the document() function. If I import "Document.xsl" in
every subsequent stylesheet, everything should be fine.

Thanks a lot for again your help,
Vince C.
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3155
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm...
2
3382
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
12
3195
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
3
2116
by: Andy | last post by:
Hi all, I'm having a problem doing an Xslt transform in code. I've done it before, so I'm not really sure why its not working. The problem is that the result of the transform is an empty...
4
1648
by: dar_imiro | last post by:
Hi, I'm trying to get rid of frames as menu holder in my html-page. I'd also like to separate the menu structure to xml and xslt. Also the actual content is divided to xml and its corresponding...
2
7361
by: Scamjunk | last post by:
I have been desperately looking for a treeview-type solution for my problem for the past three weeks and have been greatly unsuccessful. I am totally new to the world of XSLT and I *don't know*...
12
2875
by: das | last post by:
Hello all, I am using .NET XSLT to transform an XML into another XML file. All this is fine with small files, but when tested with big files (30MB) it is taking between 1hr-2hrs to just transform...
6
3708
by: John Larson | last post by:
Hi All, I am some information from INSPEC database records in XML to build a relational database of my own. I am currently trying to extract information by doing an XSLT transform of the XML...
2
4348
by: djnokturnal | last post by:
Hey guys / gals, First time posting and of course I am sure it is something that has been answered 100 times but for some reason I just cant find the answer :) First off here is the structure...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.