Jump to content

Reusing template code for different nodes


omegared

Recommended Posts

HiI am new to XSL and was hoping somebody would be kind enough to answer the following questions. I have read a number of online tutorials but they do not go above a very basic introduction. Please answer these questions in the context of using xml and xslt to seperate content and format.1)If I want to apply exactly the same template to a node which has a different name, how do I do this. At the moment I am copy/pasting the whole template and changing the nodename in 'match=nodename'. Do I have to copy the whole template everytime? Is there no way I can reuse the same template by sating something like:<apply template select =“template1”><value-of=/nodeA></apply template><apply template select =“template1”><value-of=nodeJ></apply template>2)Suppose you have two paragraphs which have identical formatting e.g. font size, colour, style except that one paragraph begins with has different alignment. At the moment I am writing two templates e.g.<xsl:template match="para1"> <fo:block text-indent="0.5in" font-size="12pt" font-family="sans-serif" line-height="15pt" space-after.optimum="12pt" text-align="right"> <xsl:value-of select="."/> </fo:block></xsl:template>and <xsl:template match="para2"> <fo:block font-size="12pt" font-family="sans-serif" line-height="15pt" space-after.optimum="12pt" text-align="right"> <xsl:value-of select="."/> </fo:block></xsl:template>Is there no way of saying apply all of the template specification in para1 to para2 but change the alignment with out repeating the same styling definitions? D I have to write separate templates every time regardless?Can someone at least direct me to the properties I should be looking at for both questions. Many thanks.

Link to comment
Share on other sites

A templte's match is an XPath expression. Therefore, the "|" XPath operator could be used to select multiple paths, thus applying the templates multiple times.Example:

<xsl:template match="para1|para2">

As for grouping other stuff. You can do this by creating a template without a match, but with a name. Example:

<template name="paras"><fo:block font-size="12pt" font-family="sans-serif" line-height="15pt"space-after.optimum="12pt"text-align="right"><xsl:value-of select="."/></fo:block></xsl:template>

And then use call-template like this:

<xsl:call-template name="paras"/>

In order to apply(basically speaking: copy) the template at the desired spot. In that particular case however, I can see this would not exactly suit you scince you're actually using the same single element with only a difference with one attribute. Hmm... that's exactly the reason why I don't like XSL-FO. It makes it complicated for such things to work. If it was XHTML with CSS, you could have used a class to define the common stuff and only add the unique though ID :) . There is a way I think. Only I'm not sure of it right now. If I think of it I'll write again.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...