XSLT: best practice for several similar templates

G

Gerald Aichholzer

Hello NG,

I have an XML document which looks like the following
example:

<document>
<section1>
<symbol>...</symbol>
<symbol>...</symbol>
<symbol>...</symbol>
</section1>
<section2>
<symbol>...</symbol>
<symbol>...</symbol>
<symbol>...</symbol>
</section2>
</document>

Each document has two sections. Each section consists
of a series of symbols. Each symbol contains several
nodes.

I have a stylesheet (written by a colleague) which
renders a document to XHTML using <xsl:apply-templates>.
Rendering a symbol depends on a global variable $size.

I would like to have a different size for symbols in
section1 than for symbols in section2.

What is the best practice to do this?

Until know I have found the following possibilities:

1. passing the size as parameter to all xsl:apply-templates
as there are many templates this would require some work

2. using different modes
about the same work as above; plus I would have duplica-
ted the templates (and will have to maintain everything
twice)

3. checking the symbol's parent
modifying the existing templates and creating a local
size-variable which content is determined by the sym-
bols parent
this looks not very flexible and elegant to me


Are there some other possibilities? What would you recommend?

thanx in advance,
Gerald
 
D

Dimitre Novatchev

Gerald Aichholzer said:
Hello NG,

I have an XML document which looks like the following
example:

<document>
<section1>
<symbol>...</symbol>
<symbol>...</symbol>
<symbol>...</symbol>
</section1>
<section2>
<symbol>...</symbol>
<symbol>...</symbol>
<symbol>...</symbol>
</section2>
</document>

Each document has two sections. Each section consists
of a series of symbols. Each symbol contains several
nodes.

I have a stylesheet (written by a colleague) which
renders a document to XHTML using <xsl:apply-templates>.
Rendering a symbol depends on a global variable $size.

I would like to have a different size for symbols in
section1 than for symbols in section2.

What is the best practice to do this?

Until know I have found the following possibilities:

1. passing the size as parameter to all xsl:apply-templates
as there are many templates this would require some work

2. using different modes
about the same work as above; plus I would have duplica-
ted the templates (and will have to maintain everything
twice)

3. checking the symbol's parent
modifying the existing templates and creating a local
size-variable which content is determined by the sym-
bols parent
this looks not very flexible and elegant to me


Are there some other possibilities? What would you recommend?


Make the global variable accomodate a node-set, containing the two size
values. Use it like this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myParam="my:myParam"
exclude-result-prefixes="myParam"
<xsl:eek:utput method="text"/>

<myParam:myParam>
<size>10</size>
<size>12</size>
</myParam:myParam>

<xsl:variable name="gblpSizes" select="document('')/*/myParam:*[1]/*"/>

<xsl:template match="/">
<xsl:apply-templates select="*/section"/>
</xsl:template>

<xsl:template match="section">
<xsl:variable name="vPos" select="position()"/>
<xsl:variable name="vSize" select="$gblpSizes[$vPos]"/>

Section <xsl:value-of select="$vPos"/>
Size: <xsl:value-of select="$vSize"/>
</xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following xml document:

<document>
<section>
<symbol>...</symbol>
<symbol>...</symbol>
<symbol>...</symbol>
</section>
<section>
<symbol>...</symbol>
<symbol>...</symbol>
<symbol>...</symbol>
</section>
</document>

the result is:

Section 1
Size: 10

Section 2
Size: 12


Hope this helped.

Cheers,
Dimitre Novatchev.
 
D

David Carlisle

Gerald Aichholzer said:
Hello NG,

I have an XML document which looks like the following
example:

<document>
<section1>
<symbol>...</symbol>
<symbol>...</symbol>
<symbol>...</symbol>
</section1>
<section2>
<symbol>...</symbol>
<symbol>...</symbol>
<symbol>...</symbol>
</section2>
</document>

Each document has two sections. Each section consists
of a series of symbols. Each symbol contains several
nodes.

I have a stylesheet (written by a colleague) which
renders a document to XHTML using <xsl:apply-templates>.
Rendering a symbol depends on a global variable $size.

I would like to have a different size for symbols in
section1 than for symbols in section2.

What is the best practice to do this?

Until know I have found the following possibilities:

1. passing the size as parameter to all xsl:apply-templates
as there are many templates this would require some work

2. using different modes
about the same work as above; plus I would have duplica-
ted the templates (and will have to maintain everything
twice)

3. checking the symbol's parent
modifying the existing templates and creating a local
size-variable which content is determined by the sym-
bols parent
this looks not very flexible and elegant to me


Are there some other possibilities? What would you recommend?

thanx in advance,
Gerald

3 is the most general: it's a besic part of xslt's design that you
always have read access to the whole tree.

XSLT2 will make 2 a bit more palatable as you can list all applicable
modes in the mode attribute of a template so you won't have to duplicate
templates. Tunnel parameters will also reduce teh amount of work doing
(1) but probably I wouldn't do either, since you are trageting css, all
you need to do is make a div for each section and then you can arrange
the size of children of the two sections with css.

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top