tags:

views:

2382

answers:

6

I have the following snippet of 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>

The if statement does not work because I haven't been able to work out the syntax for contains. How would I correctly express that xsl:if?

+1  A: 

It should be something like...

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

(not tested)

See w3schools (always a good reference BTW)

cadrian
In the lines of... ?
Leandro López
Code formatting corrected.
Tomalak
@cadrian: The curly braces are wrong.
Tomalak
Thanks for your comments!
cadrian
+11  A: 

Sure there is! For instance:

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

The syntax is: contains(stringToSearchWithin, stringToSearchFor)

Cerebrus
+2  A: 

there is indeed an xpath contains function it should look something like:

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

John Hunter
+1  A: 
<xsl:if test="not contains(hhref,'1234')">
vartec
Does the not operator work without a parentheses? If so, I didn't know that.
Cerebrus
+1  A: 

From Zvon.org XSLT Reference:

XPath function: boolean contains (string, string)

Hope this helps.

Leandro López
+1  A: 

Use the standard XPath function contains().

Function: boolean contains(string, string)

The contains function returns true if the first argument string contains the second argument string, and otherwise returns false

Dimitre Novatchev