Template Problem

Discussion in 'XML' started by Tassilo J. Klein, Aug 20, 2004.

  1. 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
     
    Tassilo J. Klein, Aug 20, 2004
    #1
    1. Advertising

  2. > 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

    --
    Rémi Peyronnet
     
    =?ISO-8859-1?Q?R=E9mi_Peyronnet?=, Aug 21, 2004
    #2
    1. Advertising

  3. 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 -
    >> 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
    >
     
    Tassilo J. Klein, Aug 22, 2004
    #3
  4. Okay, I got it...it was rather simple.

    Transforming

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


    into

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


    Thanks again :)

    Tassilo J. Klein wrote:
    > 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 -
    >
    >>> 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
    >>
     
    Tassilo J. Klein, Aug 22, 2004
    #4
  5. > >> <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.

    --
    Rémi Peyronnet
     
    =?ISO-8859-1?Q?R=E9mi_Peyronnet?=, Aug 22, 2004
    #5
  6. You are perfectly right. Your solution is superb and works brilliantly
    in all possible cases. Thanks again!!

    Regards,
    -Tassilo

    Rémi Peyronnet wrote:
    >> >> <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.
    >
     
    Tassilo J. Klein, Aug 22, 2004
    #6
  7. 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

    Tassilo J. Klein wrote:
    > You are perfectly right. Your solution is superb and works brilliantly
    > in all possible cases. Thanks again!!
    >
    > Regards,
    > -Tassilo
    >
    > Rémi Peyronnet wrote:
    >
    >>> >> <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.
    >>
     
    Tassilo J. Klein, Aug 23, 2004
    #7
  8. 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.

    --
    Rémi Peyronnet
     
    =?ISO-8859-1?Q?R=E9mi_Peyronnet?=, Aug 23, 2004
    #8
  9. 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 wrote:
    > 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.
    >
     
    Tassilo J. Klein, Aug 24, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    503
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    568
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,151
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    810
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    563
Loading...

Share This Page