Template Problem

T

Tassilo J. Klein

Hi there,

I have an XML which is displayed using a XLS file. In order to convert
the line feets (LF) into HTML specific line breaks I use a template I
found on the internet:

<xsl:template name="lf2br">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,'
')">
<xsl:value-of select="substring-before($text,'
')"/>
<br/>
<xsl:call-template name="lf2br">
<xsl:with-param name="text">
<xsl:value-of select="substring-after($text,'
')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="$text"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>

I call this template using XLS like this:

<xsl:call-template name="lf2br">
<xsl:with-param name="text" select="."/>
</xsl:call-template>

Now the text inside the tag is formatted nicely and ready for output.
But the problem is, the text contains some tags like <Omega/> which
translate into special characters but which will not be processed due to
the hack with the lf2br routine.

This one will be omitted since not using <xsl:apply-templates/>:

xsl:template match="Omega">
<font style="font-family: Times">
<xsl:text>
Ω
</xsl:text>
</font>
</xsl:template>


I am quite new to this but I want to combine those two features - using
the lf2br routine and have those tags inside the text processed. Does
anybody know how to do that?

Thanks in advance,
Tassilo
 
?

=?ISO-8859-1?Q?R=E9mi_Peyronnet?=

I am quite new to this but I want to combine those two features - using
the lf2br routine and have those tags inside the text processed. Does
anybody know how to do that?

One solution would be to use apply-templates, and redefine the template
"text()" instead :

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

<xsl:template match="text()">
<xsl:call-template name="lf2br">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>

<xsl:template match="Omega">
....
</xsl:template>

Hth
 
T

Tassilo J. Klein

Rémi Peyronnet wrote:

Merci bien Remi, you helped me a lot...BUT, your solutions also regards
the space between the tags.
For instance, if I have

<Text>First Test</Text>


<Text>Second Test</Text>

it will look like:

First Test


Second Test

because of the space between the tags!

Similar:

<Text>First Test</Text>
<Text>Second Test</Text>

will look like that:

First Test
Second Test


But I don't want the template to regard the space between the tags. Is
there some way to overcome this - some template that doesn't mind the
sapce between the tags. Then it would just be perfect.

Regards and thanks for your help so far!!!

- Tassilo -
 
?

=?ISO-8859-1?Q?R=E9mi_Peyronnet?=

<xsl:template match="/text()">

This may work for your case, but be carefull that it processes only
special text elements (in root element).

Thus something like :
<foo>
<bar>
sometext here <Omega /> sometext here
and replace this lf by a br tag
</bar>
</toto>
will certainly not behave as expected.

You should try one of these solutions instead :
- use the magic <xsl:strip-space elements="*" />
- if strip-space is not supported by your processor (should not be
frequent I guess), put the call-template into this if statement :
<xsl:if test="string-length(translate(.,'
','')) != 0"> ,
this won't process text nodes with only whitespaces in it.
 
T

Tassilo J. Klein

You are perfectly right. Your solution is superb and works brilliantly
in all possible cases. Thanks again!!

Regards,
-Tassilo
 
T

Tassilo J. Klein

Well Remi and others, with my perfect considered solution something is
wrong. Slowly all this seems like a Sysiphus' job to me!! At first I had
to manipulate the root template match sequence in order to get the root
element read (otherwise it will be simply ignored..):


<xsl:template match="/">
[..]
</xsl:template>

transformed into

<xsl:template match="/*">
[..]
</xsl:template>

At first I thought, that it works but I am now I am not sure anymore
since of the following. Usually I run Mozilla Firefox to look at the
conversion of my XML document via XSLT. In this case everything is
formatted correctly. But the other day I tried to translate my XML/XSL
into a HTML file using Xalan and XMLSpy and both produced different
(undesired) results than Firefox. Maybe the root element is the source
of all evil?


I have a XML file looking like that:

<Page>
<Header/>
<Information/>
<Table/>
</Page>

XSLT is supposed to create a <table > when reading the root element (
<Page>) enshrining all other tags. Then for each other tag found a table
is created.

It is supposed to look like that which it in fact does in Firefox but
not in Xalan and all other systems :'(.


<table Page>

<table Header>
</table Header>

<table Information>

<table Table>
</table Table>

</table Information>

</table Page>


In Xalan the result will be something like that.

<table Page>
</table Page>

<table Header>
</table Header>

<table Information>
</table Information>

<table Table/>
</table Table>


Any idea?

Thanks in advance!

-Tassilo
 
?

=?ISO-8859-1?Q?R=E9mi_Peyronnet?=

Tassilo J. Klein a écrit :
<xsl:template match="/*">
[..]
</xsl:template>

Well, it begins to be too complicated to discuss it without a real
example. You should post source xml + your current xsl + expected
result, in order to get help.
 
T

Tassilo J. Klein

Okay, sorry ! Errare humanum est - I did some mistake :). Thanks to
god I don't have to bother you with my XSLT.

Regards,
Tassilo

Rémi Peyronnet said:
Tassilo J. Klein a écrit :
<xsl:template match="/*">
[..]
</xsl:template>

Well, it begins to be too complicated to discuss it without a real
example. You should post source xml + your current xsl + expected
result, in order to get help.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top