Is there an XSL "contains" directive?

Xslt

Xslt Problem Overview


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?

Xslt Solutions


Solution 1 - Xslt

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)

Solution 2 - Xslt

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

Solution 3 - Xslt

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>
</xsl:for-each>

Solution 4 - Xslt

It should be something like...

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

(not tested)

See w3schools (always a good reference BTW)

Solution 5 - Xslt

From Zvon.org XSLT Reference:

XPath function: boolean contains (string, string) 

Hope this helps.

Solution 6 - Xslt

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuyView Question on Stackoverflow
Solution 1 - XsltCerebrusView Answer on Stackoverflow
Solution 2 - XsltDimitre NovatchevView Answer on Stackoverflow
Solution 3 - XsltJohn HunterView Answer on Stackoverflow
Solution 4 - XsltcadrianView Answer on Stackoverflow
Solution 5 - XsltLeandro LópezView Answer on Stackoverflow
Solution 6 - XsltvartecView Answer on Stackoverflow