Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kim Andersen 1447 posts 2196 karma points MVP
    Aug 10, 2011 @ 10:50
    Kim Andersen
    0

    Weird problem using apply-templates

    I have a pretty strange problem in one of my projects. I have created a node width a property called "title". This property is based on the "Simple editor"-data type.

    When I want to render the content of the editor I usuallt use something like this:

    <xsl:template name="myName">
    <xsl:param name="itm" />
    <xsl:apply-templates select="$itm/title[normalize-space()]" />
    </xsl:template>

    <xsl:template match="title">
    <h2>
    <xsl:value-of select="." disable-output-escaping="yes" />
    </h2>
    </xsl:template>

    The above code is placed in a xslt-file and imported in another xslt-file. So I'm calling the template from the "main"-xslt file like this:

    <xsl:call-template name="myName">
    <xsl:with-param name="itm" select="$itm" />
    </xsl:call-template>

    The XML of the $itm/title looks like this:

    <title>
        <strong>Order</strong> now
     </title>

    But here's the weird part:

    When using the apply-templates method the <strong>-thml elements are shown on the frontend.

    But if I'm using a value-of like this:

    <h2>
    <xsl:value-of select="$itm/title" disable-output-escaping="yes" />
    </h2>

    The text is rendered the right way, with bold "Order".

    What's going on here?

    /Kim A

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 10, 2011 @ 10:59
    Chriztian Steinmeier
    0

    Hi Kim,

    The so-called "strong" tags were never actual elements - they are plain text (just as is the problem with Rich Text Editor content).

    If you need to handle them as XML, you can use the uComponents Parse() extension function to convert the content into XML. (Yes, I'm actually writing about that at the moment... :-)

    /Chriztian 

  • Lennart Stoop 304 posts 842 karma points
    Aug 10, 2011 @ 11:04
    Lennart Stoop
    1

    I would think it has something to do with the output type of the XSLT (method=html or method=xml).

    Apply templates should work if you set the method to html, right ?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 10, 2011 @ 11:14
    Chriztian Steinmeier
    2

    To clarify:

    The XML content of the $itm/title actually looks like this:

    <title>&lt;strong&gt;Order&lt;/strong&gt; now</title>

    or (which is exactly the same):

    <title><![CDATA[<strong>Order</strong> now]]></title>

    But neither of those are "real" XML and thus, xsl:apply-templates will effectively apply the built-in template for text() and output the text, while maintaining escaping of entities.

    Only when you bypass the escaping (using disable-output-escaping on the xsl:value-of instruction) will you be able to "convert" the escaped tags to something that the browser will interpret as HTML tags. But the XSLT processor never actually "see" them as <strong> elements.

    /Chriztian

  • Kim Andersen 1447 posts 2196 karma points MVP
    Aug 10, 2011 @ 11:39
    Kim Andersen
    0

    Hmm....

    I actualy think that I've found the solution (before tryuing out your suggestions guys - sorry :) ).

    In my "main"-xslt file I've imported some more xslt files just as I have imported the one I had the problem with above. And in one of those imported xslt files there was another template that matched "title" as well. And of course this template was in a file imported later in the "main"-xslt file. Meaning that the template I where actually hitting was the one in another file who didn't have the disable-output-escaping="yes". I tried setting a mode on my title-template above (shown below), and now everything works just as I wanted.

    <xsl:templatename="myName">
     
    <xsl:paramname="itm"/>
     
    <xsl:apply-templatesselect="$itm/title[normalize-space()]"mode="myMode"/>
    </xsl:template>

    <xsl:templatematch="title" mode="myMode">
       
    <h2>
         
    <xsl:value-ofselect="."disable-output-escaping="yes"/>
       
    </h2>
    </xsl:template>

    Thanks for your suggestions anyway :)

    /Kim A

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 10, 2011 @ 11:51
    Chriztian Steinmeier
    0

    Great Kim,

    Sometimes we just try to fix the wrong problem :)

    I didn't catch what you we're *actually* baffled about...

    The mode parameter is a good way to solve conflicting template issues, because you can use your own mode names, and essentially explain to yourself (or future readers of the code) what's going on.

    /Chriztian  

  • Lennart Stoop 304 posts 842 karma points
    Aug 10, 2011 @ 11:51
    Lennart Stoop
    0

    Guess we were already digging to deep, still some lessons learned tho.

    Glad you got it working!

Please Sign in or register to post replies

Write your reply to:

Draft