2009-02-20 74 views
49

我有XSL的下面的代码片段:是否有XSL“包含”指令?

<xsl:for-each select="item"> 
    <xsl:variable name="hhref" select="link" /> 
    <xsl:variable name="pdate" select="pubDate" /> 
    <xsl:if test="hhref not contains '1234'"> 
     <li> 
     <a href="{$hhref}" title="{$pdate}"> 
      <xsl:value-of select="title"/> 
     </a> 
     </li> 
    </xsl:if> 
    </xsl:for-each> 

if语句不工作,因为我一直没能制定出语法包含。我将如何正确表达xsl:if?

回答

98

当然有!例如:

<xsl:if test="not(contains($hhref, '1234'))"> 
    <li> 
    <a href="{$hhref}" title="{$pdate}"> 
     <xsl:value-of select="title"/> 
    </a> 
    </li> 
</xsl:if> 

的语法是:contains(stringToSearchWithin, stringToSearchFor)

1

它应该是这样的......

<xsl:if test="contains($hhref, '1234')"> 

(未测试)

w3schools(总是一个很好的参考BTW)

+0

在行......的? – 2009-02-20 15:22:44

+0

更正了代码格式。 – Tomalak 2009-02-20 15:27:20

6

确实是有一个XPath包含的功能就应该是这个样子:

<xsl:for-each select="item"> 
<xsl:variable name="hhref" select="link" /> 
<xsl:variable name="pdate" select="pubDate" /> 
<xsl:if test="not(contains(hhref,'1234'))"> 
    <li> 
    <a href="{$hhref}" title="{$pdate}"> 
     <xsl:value-of select="title"/> 
    </a> 
    </li> 
</xsl:if> 

1
<xsl:if test="not contains(hhref,'1234')"> 
6

使用标准XPath函数contains()

功能布尔包含(字符串,字符串)

如果第一个字符串参数包含第二个参数字符串contains函数返回真,否则返回假