2012-03-29 55 views
1

我正在使用XSLT處理器腳本從外部源獲取數據。 現在我想測試某個值是否爲空,然後返回空字段。 這是我現在在我的XSL樣式表中使用的代碼。使用XSL樣式表測試值是否爲空XML

<?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> 

我想有它返回一個鏈接到Facebook的事件鏈接的「網站」值的條件語句。但是,當沒有填充在系統中的數據從何而來,它不應該顯示(我參加)

這怎麼能與一個XSL測試或某種條件語句來預製的聯繫?

回答

2

我想對「網站」值 它返回一個鏈接到Facebook的事件鏈接條件語句。但是,當沒有被填充在數據來自系統 ,它不應該顯示 鏈接(我參加)

一個簡單而優雅的方式在XSLT做到這一點:

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

然後讓這個模板

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

在這裏,您不必驗證website元素是否存在 - XSLT處理器爲您完成這項工作。通過這種方式,我們不必編寫任何明確的條件指令,而且代碼簡單,簡潔易讀。

1

嘗試此鏈接顯示:

<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>