Copied to clipboard

Flag this post as spam?

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


  • James Smyth 4 posts 24 karma points
    Nov 03, 2011 @ 16:47
    James Smyth
    0

    Remove Comma from list if DocType property is absent.

    Right I have a macro that display the description of an image. The problem is, not all the properties are mandatory and sometimes they are left blank.

    Below I get the properties in a macro and seperate them with a comma (,).

    Is there a way of removing the comma if a property is left blank?

     

    <div class="description">
                           <span><xsl:value-of select="title"/>, <xsl:value-of select="year"/>, <xsl:value-of select="medium"/>, <xsl:value-of select="dimensions"/></span>
                       </div>

     

    Thanks in advance

     

    James

  • Anthony pj 40 posts 63 karma points
    Nov 03, 2011 @ 16:59
    Anthony pj
    0

    Cannt  you use

       <xsl:if test="CONDITION">

    </xsl:if>

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 03, 2011 @ 17:02
    Tom Fulton
    0

    Hi James,

    Yes, there's a few ways you could handle this.  I think the below should work and is probably the best:

    <xsl:template match="/">

    <div class="description">
      <span>
        <xsl:apply-templates select="title" mode="comma" />
        <xsl:apply-templates select="year" mode="comma" />
        <xsl:apply-templates select="medium" mode="comma" />
        <xsl:apply-templates select="dimensions" mode="comma" />
      </span>
    </div>

    </xsl:template>

    <!-- Match anything that's not empty -->
    <xsl:template match="*[normalize-space()]" mode="comma">
    <xsl:value-of select="."/>,
    </xsl:template>

    <!-- Match empty elements (do nothing) -->
    <xsl:template match="*[not(normalize-space())]" mode="comma"/>

    If you were doing different things with each element I'd reccomend you do one template for each of them, but since you are just writing out the text and a comma I tried just using one template.

    Ex:  <xsl:template match="title"><strong><xsl:value-of select="."/></strong>, </xsl:template>

    -Tom

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 03, 2011 @ 17:03
    Tom Fulton
    0

    And as Anthony suggested wrapping each line in an IF statement would work too, I prefer something like the above though for cleanliness :)

  • James Smyth 4 posts 24 karma points
    Nov 08, 2011 @ 18:12
    James Smyth
    0

    Hey guys, thanks a bunch for your answers,

    I went with the simplist option of:

     

    <div class="description">
                      <span>

                        <xsl:value-of select="title"/>

                        <xsl:if test="year != ''">
                          <xsl:value-of select="year"/>
                        </xsl:if>

                        <xsl:if test="medium != ''">
                          <xsl:value-of select="medium"/>
                        </xsl:if>

                        <xsl:if test="dimensions != ''">
                          <xsl:value-of select="dimensions"/>
                        </xsl:if>

                      </span>
                    </div>

    This works fine.

    Again, thanks!

Please Sign in or register to post replies

Write your reply to:

Draft