Copied to clipboard

Flag this post as spam?

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


  • Kris Dyson 54 posts 79 karma points
    Mar 09, 2010 @ 20:04
    Kris Dyson
    0

    MS T-SQL Coalesce XSLT equivalent

    Hi there, I have an XSLT macro which I need to adapt so that it can also retrieve data from another doc type, and output virtually the same xhtml.

    So I would like to do this

                    <xsl:if test="data [@alias='carouselImage'] != ''">
                      <xsl:variable name="mediaId" select="number(data [@alias='carouselImage']/text())" />
                    </xsl:if>
                    <xsl:if test="data [@alias='thumbnail'] != ''">
                      <xsl:variable name="mediaId" select="number(data [@alias='thumbnail']/text())" />
                    </xsl:if>
                    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />

    .... but you can't in XSLT, so I wondered whether it's possible to get XSLT to assign the variable value based on the first non-empty item? e.g.

    <xsl:variable name="mediaId" select="number( coalesce( data [@alias='carouselImage'], data [@alias='thumbnail'] ) )" />

     if only that function exists! OK, so now I'm thinking I need to create a separate macro... does anyone know of a better way to do this?  I could just rename the aliases in both doc types to be the same, however, that could cause a conflict.

    thanks

    Kris 

     

  • Tommy Poulsen 514 posts 708 karma points
    Mar 09, 2010 @ 20:23
    Tommy Poulsen
    1

    Hi Kristan

    you can make the content of the variable conditional

    <xsl:variable name="mediaId">
    <xsl:choose>
    <xsl:when test="
    data [@alias='carouselImage'] != ''>
    ... output whatever ...
    </xsl:when>
    ...
    <xsl:otherwise>
    ...
    </xsl:otherwise>
     </xsl:choose>
    </xsl:variable>
  • jaygreasley 416 posts 403 karma points
    Mar 09, 2010 @ 20:26
    jaygreasley
    0

    Hey Kris,

    I just wondered why you can't have 2 macros, one for each doctype?

    Jay

  • Peter Dijksterhuis 1442 posts 1722 karma points
    Mar 09, 2010 @ 20:27
    Peter Dijksterhuis
    0

    Hi Kris,

     

    you can use a xsl:choose perhaphs:

     

    <xsl:variable name="mediaId">
    <xsl:choose>
    <xsl:when test="data [@alias='carouselImage'] != ''">
    <xsl:value-of select="number(data [@alias='carouselImage']/text())"/>
    </xsl:when>
    <xsl:otherwise>

    <xsl:choose>
    <xsl:when test="data [@alias='thumbnail'] != ''">
    <xsl:value-of select="number(data [@alias='thumbnail']/text())"/>
    </xsl:when>
    <xsl:otherwise>
    1234
    </xsl:otherwise>
    </xsl:choose>

    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

     

    HTH,

    Peter

  • Kris Dyson 54 posts 79 karma points
    Mar 09, 2010 @ 21:02
    Kris Dyson
    0

    Hi guys - this is great, I didn't realise you could nest a choose inside a variable element.

    I could make two macros, but they are so similar - so it'll probably be a bit easier to just maintain one.

    thanks again

    k

Please Sign in or register to post replies

Write your reply to:

Draft