subclassing in XSLT

A

Andy Fish

Hi,

I'm trying to achieve something like subclassing in XSLT, by which I mean
that I have stylesheet A and I want to make a more specialised "subclass"
called stylesheet B. which inherits most of the functions but overrides some
templates.

the closest I can think of getting is to write template B including template
A, and then put templates in B with the same "match" clause but with a
higher priority than A. Unfortunately, a key feature would be to invoke the
superclass method as part of the overridden method, and I can't see any way
to do this.

TIA for Any ideas

Andy
 
M

Martin Honnen

Andy Fish wrote:

the closest I can think of getting is to write template B including template
A, and then put templates in B with the same "match" clause but with a
higher priority than A. Unfortunately, a key feature would be to invoke the
superclass method as part of the overridden method, and I can't see any way
to do this.

Doesn't
<xsl:apply-imports />
do what you want?
 
P

Patrick TJ McPhee

% the closest I can think of getting is to write template B including template
% A, and then put templates in B with the same "match" clause but with a
% higher priority than A. Unfortunately, a key feature would be to invoke the

xsl:import does this implicitly.


% superclass method as part of the overridden method, and I can't see any way
% to do this.

Once you've used xsl:import, you can invoke the template from the imported
stylesheet using xsl:apply-imports.

<!-- stylesheet A.xsl -->
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:eek:utput method='text'/>

<xsl:template match='A'>
<xsl:text>standard processing of A.</xsl:text>
</xsl:template>
</xsl:stylesheet>


<!-- stylesheet B.xsl -->
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<!-- this must come first! -->
<xsl:import href='A.xsl'/>

<xsl:template match='A'>
<xsl:text>Specialised processing of A, followed by </xsl:text>
<xsl:apply-imports/>
</xsl:template>
</xsl:stylesheet>

<!-- A.xml -->
<A/>

of course in the real world I would use names like x.xsl and y.xsl.
Anyway, if you process A.xml using A.xsl, you get this output

standard processing of A.

while if you process A.xml using B.xsl, you get this

Specialised processing of A, followed by standard processing of A.
 
D

David Carlisle

I guess the latter remains true in XSLT 2.0, too.

yes although in 2 you can use next-match rather than apply-imports (with
or without using xsl:import) this has rather different behaviour on edge
cases.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top