Copied to clipboard

Flag this post as spam?

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


  • Martin 278 posts 662 karma points
    Aug 12, 2011 @ 11:56
    Martin
    0

    Remove <li> tag if field is not populated

    Hi all, im having some trouble with my XSLT. 

    I have used the Embedded Content Package to create a partners contact page.

    My issue is with removing the the <li> tag if the field has been left empty. Just now the <li> is collpased, so it doesnt affect the appearance, but I would like to not to output the <li> tag if it is not required.

    My xslt is.

     

    Any help would be grateful.

    Martin

     

    <xsl:for-each select="$currentPage/partners/data/item">
    <div class="tal-8">
      <ul class="partnerAdd">
        <li class="location"><xsl:value-of select="location"/></li>
        <li><xsl:value-of select="partnersName"/></li>
        <li><xsl:value-of select="address1"/></li>
        <li><xsl:value-of select="address2"/></li>
        <li><xsl:value-of select="city"/></li>
        <li><xsl:value-of select="state"/></li>
        <li><xsl:value-of select="country"/></li>
        <li><xsl:value-of select="areaCode"/></li>
        <li>T: <xsl:value-of select="telNo"/><span style="display:none;">NoSkype</span></li>
        <li>E:       
          <a>
            <xsl:attribute name="href">mailto:<xsl:value-of select="email"/></xsl:attribute>
            <xsl:value-of select="email"/>
          </a>
        </li>  
        <li>W: 
          <href="http://{web}" target="_blank">
            <xsl:value-of select="web"/>
          </a>
        </li>
      </ul>
    </div>      
    </xsl:for-each>
  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Aug 12, 2011 @ 12:31
    Chriztian Steinmeier
    1

    Hi Martin,

    Though you could get away with a simple <xsl:if> to test whether a field has content, the "XSLT way" of doing something like this, is more like this:

    <xsl:template match="/">
        <xsl:apply-templates select="$currentPage/partners/data/item" />
    </xsl:template>
    
    <xsl:template match="item">
        <div class="tal-8">
            <ul class="partnerAdd">
                <xsl:apply-templates select="location" />
                <xsl:apply-templates select="partnersName" />
                <xsl:apply-templates select="address1" />
                <xsl:apply-templates select="address2" />
                <xsl:apply-templates select="city" />
                <xsl:apply-templates select="state" />
                <xsl:apply-templates select="country" />
                <xsl:apply-templates select="areaCode" />
                <xsl:apply-templates select="telNo" />
                <xsl:apply-templates select="email" />
                <xsl:apply-templates select="web" />
            </ul>
        </div>
    </xsl:template>
    
    <!-- Generic template for most items -->
    <xsl:template match="item/*">
        <li>
            <xsl:value-of select="." />
        </li>
    </xsl:template>
    
    <!-- Telephone -->
    <xsl:template match="telNo">
        <li>
            <xsl:text>T: </xsl:text>
            <xsl:value-of select="." />
            <span style="display:none">NoSkype</span>
        </li>
    </xsl:template>
    
    <!-- Email -->
    <xsl:template match="email">
        <li>
            <a href="mailto:{.}">
                <xsl:value-of select="." />
            </a>
        </li>
    </xsl:template>
    
    <!-- Website -->
    <xsl:template match="web">
        <li>
            <a href="http://{.}" target="_blank">
                <xsl:value-of select="." />
            </a>
        </li>
    </xsl:template>
    
    <!-- Make sure to suppress empty fields -->
    <xsl:template match="data/item/*[not(normalize-space())]" priority="1">
        <!-- No output -->
    </xsl:template>

    /Chriztian 

  • Martin 278 posts 662 karma points
    Aug 12, 2011 @ 12:56
    Martin
    0

    Thanks Chriztian, 

    The generic template seem to override the email, telephone & web.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 12, 2011 @ 13:16
    Michael Latouche
    0

    Hi Martin,

    Have you tried putting the generic template as last in the xslt file?

    Cheers,

    Michael.

    Edit: Or set the priority to "2" on the not generic templates?

  • Martin 278 posts 662 karma points
    Aug 12, 2011 @ 13:20
    Martin
    0

     

    Yeah, ive tried moving the generic template to after the email, tele, web templates.

    It still overrides the other templates.

    EDIT: Thanks Micheal. Setting the priority on the other templates worked. Thanks

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Aug 12, 2011 @ 13:38
    Michael Latouche
    0

    Great!

    Can you maybe mark that post as an answer, so it can help others with similar problems? Thx!

    Cheers,

    Michael.

Please Sign in or register to post replies

Write your reply to:

Draft