1

I'm using an XSLTprocessor script to get data from an external source. Now I would like to test wether a certain value is empty and then return a empty field. this is the code I'm using now in my XSL stylesheet.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html"/>

<xsl:template match="/">

  <table border="1" style="width:600px;margin-top:20px">
    <tr>
      <th align="left" style="text-transform: uppercase; font-size: 14px; color: white;font-weight: normal;">Date</th>
      <th align="left" style="text-transform: uppercase; font-size: 14px; color: white;font-weight: normal;">Location</th>
      <th align="right" style="text-transform: uppercase; font-size: 14px; color: white;font-weight: normal;">City</th>
      <th align="right" style="text-transform: uppercase; font-size: 14px; color: white;font-weight: normal;">Country</th>
      <th align="right" style="text-transform: uppercase; font-size: 14px; color: white;font-weight: normal;">Facebook</th>
    </tr>
    <xsl:for-each select="//item">
    <tr>
      <td style="text-align:left;"><xsl:value-of select="date" /></td>
      <td style="text-align:left;color:#ccc;"><xsl:value-of select="location" /></td>
      <td style="text-align:right;"><xsl:value-of select="city "/></td>
      <td style="text-align:right;"><xsl:value-of select="country" /></td>
      <td style="text-align:right;"><a>
<xsl:attribute name="href">
<xsl:value-of select="website" />
</xsl:attribute>
<xsl:attribute name="target">new</xsl:attribute>
I'm Attending
</a></td>
    </tr>
    </xsl:for-each>
  </table>
</xsl:template> 
</xsl:stylesheet>

I would like to have a conditional statement on the "website" value which returns a link to a Facebook event link. But when nothing is filled in the system where the data comes from, it should not display the link (I'm Attending)

How can this be preformed with a XSL test or some kind of conditional statement?

2 Answers 2

2

I would like to have a conditional statement on the "website" value which returns a link to a Facebook event link. But when nothing is filled in the system where the data comes from, it should not display the link (I'm Attending)

A simple and elegant way to do this in XSLT:

<xsl:apply-templates select="website[text()]"/>

Then have this template:

<xsl:template match="website">
  <td style="text-align:right;">
    <a href="{.}" target="new">I'm Attending</a>
  </td>
</xsl:template>

Here you don't have to verify if the website element exists -- the XSLT processor does this work for you. In this way we don't have to write any explicit conditional instructions and the code is simple, short and readable.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this for link displaying:

<xsl:if test="website/text()">
    <a>
    <xsl:attribute name="href">
        <xsl:value-of select="website" />
    </xsl:attribute>
    <xsl:attribute name="target">new</xsl:attribute>
    I'm Attending
    </a>
</xsl:if> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.