Conditionalized typesetting

P

patrik.nyman

I have several texts that are to be typeset with minor
differences. I do not want to duplicate any xsl-code,
so I'm trying to find a way to reuse code, but have
changes implemented when applicable. One approach is
of course to have a specific xsl-file for every text,
and import the more general file into it. Another
approach I have thought of, is to have just one xsl-file
and conditionalize it. In the xml-files I have something
like <text work="kerner">, <text work="kerner">, etc.,
and in the xsl-file templates like

<xsl:template match="pb">
<xsl:choose>
<xsl:when test="//@verk='kerner'">
<p class="pagina"><xsl:value-of select="@n"/></p>
</xsl:when>
<xsl:when test="//@verk='kalm'">
<p class="pagina">( <xsl:value-of select="@n"/> )</p>
</xsl:when>
</xsl:choose>
</xsl:template>

This works fine, but I'd be glad to get some input on
which method is preferrable, or if there might be other
ways to do this. What are your experiences?
 
D

David Carlisle

I have several texts that are to be typeset with minor
differences. I do not want to duplicate any xsl-code,
so I'm trying to find a way to reuse code, but have
changes implemented when applicable. One approach is
of course to have a specific xsl-file for every text,
and import the more general file into it. Another
approach I have thought of, is to have just one xsl-file
and conditionalize it. In the xml-files I have something
like <text work="kerner">, <text work="kerner">, etc.,
and in the xsl-file templates like

<xsl:template match="pb">
<xsl:choose>
<xsl:when test="//@verk='kerner'">
<p class="pagina"><xsl:value-of select="@n"/></p>
</xsl:when>
<xsl:when test="//@verk='kalm'">
<p class="pagina">( <xsl:value-of select="@n"/> )</p>
</xsl:when>
</xsl:choose>
</xsl:template>

This works fine, but I'd be glad to get some input on
which method is preferrable, or if there might be other
ways to do this. What are your experiences?

the tests starting with // search the entire document looking for an
attribute of that name, so they are fairly expensive. You may be better
to move them to a global variable (although probably your xslt system's
optimiser will do that anyway). Although I suspect that you meant to
test the attribute o the current pb element ? or the document element
?if so the test wants to be test="@work='kerner'] or
test="/*/@work='kerner']

whenever there is a template that just consists of a choose it's often
more convenient (and perhaps more efficient) to write it as multiple
templates

<xsl:template match="pb[@verk='kerner']">
<p class="pagina"><xsl:value-of select="@n"/></p>
</xsl:template>

<xsl:template match="pb[@verk='kalm']">
<p class="pagina">( <xsl:value-of select="@n"/> )</p>
</xsl:template>



David
 
P

patrik.nyman

David said:
the tests starting with // search the entire document looking for an
attribute of that name, so they are fairly expensive. You may be better
to move them to a global variable (although probably your xslt system's
optimiser will do that anyway). Although I suspect that you meant to
test the attribute o the current pb element ? or the document element
?if so the test wants to be test="@work='kerner'] or
test="/*/@work='kerner']
Thanks for this remark. Yes, I want to test the document element,
so the /*/@work='kerner' construction would be fine, since there are
several
templates that should be conditionalized. Should I take it that you
think this
is a good method for accomplishing my goal? Or are there any drawbacks?

Or perhaps a better way to do it?

/Patrik Nyman
 
D

David Carlisle

templates that should be conditionalized. Should I take it that you
think this
is a good method for accomplishing my goal? Or are there any drawbacks?

Or perhaps a better way to do it?

/Patrik Nyman

it's fine, although I'd use multiple templates rather than an xs:choose.
it looks neater and can be more efficient.

<xsl:template match="*">
<xsl:choose>
<xsl:when test="self::aaa">...
<xsl:when test="self::bbb">...
....

is more or less the same as

<xsl:template match="aaa">...
<xsl:template match="bbb">...

but in the first case, your system probably has to do a linear search of
the node against each test so expected time proportional to the number
of branches.

in the second case the system probably hashes the templates against the
match name at compile time so at run time doesn't have to test every
template.

this is of course all highly dependent on the processor, the processor
could in theory rewrite either form to the same internal expresssion, or
might not hash the templates or...

unless you have hundreds of choices in the choose it probably makes no
observable time difference, I think main attraction of having more
templates and less choose blocks is that it's just more the "xsl way" :)
 
P

patrik.nyman

David said:
it's fine, although I'd use multiple templates rather than an xs:choose.
it looks neater and can be more efficient.

<xsl:template match="*">
<xsl:choose>
<xsl:when test="self::aaa">...
<xsl:when test="self::bbb">...
...

is more or less the same as

<xsl:template match="aaa">...
<xsl:template match="bbb">...

but in the first case, your system probably has to do a linear search of
the node against each test so expected time proportional to the number
of branches.

in the second case the system probably hashes the templates against the
match name at compile time so at run time doesn't have to test every
template.

this is of course all highly dependent on the processor, the processor
could in theory rewrite either form to the same internal expresssion, or
might not hash the templates or...

unless you have hundreds of choices in the choose it probably makes no
observable time difference, I think main attraction of having more
templates and less choose blocks is that it's just more the "xsl way" :)

Thanks a lot for these clarifications.

/Patrik Nyman
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top