Copied to clipboard

Flag this post as spam?

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


  • David W. 159 posts 284 karma points c-trib
    Sep 29, 2011 @ 22:50
    David W.
    0

    Conditional markup

    How can I encapsulate content with some markup, based on a condition? The following code fails because it's invalid xslt. Surely there's an easy way to do this?

    <xsl:if test="1=1">

    <div>123

    </xsl:if>

    content goes here

    <xsl:if test="1=1">

    456</div>

    </xsl:if>

    Thanks.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Sep 29, 2011 @ 23:12
    Morten Bock
    1

    Just to make sure that you get the right answer, I'm guessing that the above example is not exactly what you want? :)

    As you say, the above won't work, because it is not a valid xml structure. So a way to do it could be this:

    <xsl:variable name="content" select="xpathhere..."/>
    <xsl:choose>
    <xsl:when test="1=1">
    <div>123<xsl:value-of select="$content"/>456</div>
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="$content"/>
    </xsl:otherwise>
    </xsl:choose> 

    Does that solve your problem?

  • David W. 159 posts 284 karma points c-trib
    Sep 29, 2011 @ 23:14
    David W.
    0

    By the looks of it, that should work. However is that the "standard way of solving this issue? Potentially the "content" part could be a komplex and large markup that I would not like to duplicate.

    Works good enough for me here though.

    Thanks.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Sep 29, 2011 @ 23:17
    Morten Bock
    0

    You can solve that by using <xsl:call-template name="complexstuff"/> where the complex stuff goes. Then you have a singe template that has your markup, and you just call it when you need it.

  • David W. 159 posts 284 karma points c-trib
    Sep 29, 2011 @ 23:18
    David W.
    0

    Ok. Thank you.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Sep 29, 2011 @ 23:18
    Morten Bock
    0

    As far as a "standard way of doing it", I don't know if there is such a thing. Depends on the exact usecase, and how you like structuring stuff. But it is 'a' way that I have used before.

  • Rodion Novoselov 694 posts 859 karma points
    Sep 30, 2011 @ 00:22
    Rodion Novoselov
    0

    <
    xsl:text disable-output-escaping="yes" >
          <![CDATA[<div>123]]>
    </xsl:text>
    <xsl:text disable-output-escaping="yes" >
          <![CDATA[456</div>]]>
    </xsl:text>
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 30, 2011 @ 00:38
    Chriztian Steinmeier
    1

    Hi Sledger,

    Because an XSLT stylesheet is an XML file itself, it has to be valid and well-formed (as Morten said), which means that the goodbad old PHP/ASP way of opening a tag in one place while closing it in another, is out of the question. That's actually a good thing - so yes, that is the way you do it - and you can always use separate templates to have it be a non-issue, e.g.:

    <xsl:template match="/">
        <xsl:variable name="needsWrapper" select="boolean($currentPage/someProperty = 42)" />
        <xsl:variable name="nodes" select="$currentPage/Event" />
    
        <xsl:choose>
            <xsl:when test="$needsWrapper">
                <div class="wrapUsUp">
                    <xsl:apply-templates select="$nodes" />
                </div>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="$nodes" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="Event">
        <article class="event">
            <h1><xsl:value-of select="@nodeName" /></h1>
            <time>...</time>
            <!-- etc. -->
        </article>
    </xsl:template>

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft