Jump to content

Help with multiple contains issue


digitalterry

Recommended Posts

I have the following situation and I cannot figure out the proper approach to this.I have a list of URL patterns that I want to do something different with in my HTML.Example:www.mydomain.com192.168.1.101www.myotherdomain.com:12495My XSLT is processing a field called "the_url".I want to write a test condition to determine if ANY one of the items are in "the_url".<xsl:variable name="the_list">www.mydomain.com, 192.168.1.101, www.myotherdomain.com:12495</xsl:variable>The condition I am checking is:Test if "the_url" contains any one of the items in "the_list""the_list" is not a fixed list. It may grow over time to include more items.Can someone provide an example of how to achieve this? I understand how to do a simple contains, but the multiple (unknown quantity of values) has me stumped.Thanks!Terry

Link to comment
Share on other sites

If you use XSLT 2.0 then simply do e.g.

<xsl:if test="the_url = tokenize($the_list, ',\s*')">

or simply make sure your variable is a sequence of string with e.g.

<xsl:variable name="the_list" select="('www.mydomain.com', '192.168.1.101', 'www.myotherdomain.com:12495')"/><xsl:if test="the_url = $the_list">

With XSLT 1.0 you might want to do e.g.

<xsl:variable name="the_list" select="'|www.mydomain.com|192.168.1.101|www.myotherdomain.com:12495|'"/><xsl:if test="contains($the_list, concat('|', the_url, '|'))">

That requires changing the value of the variable obviously to some different format.

Link to comment
Share on other sites

If you use XSLT 2.0 then simply do e.g.
<xsl:if test="the_url = tokenize($the_list, ',\s*')">

Thanks for your reply. I am using XSLT 2.0.When I used your example for XSLT 2.0, the test fails.
<xsl:variable name="the_list">www.mydomain.com,192.168.1.101,www.myotherdomain.com:12495</xsl:variable><xsl:choose><xsl:when test="$the_url = tokenize($the_list, ',\s*')">it works!</xsl:when><xsl:otherwise>**<xsl:value-of select="$the_url"/>**</xsl:otherwise></xsl:choose>

The value that prints for $the_url is:

www.mydomain.com:10204/Wiki%20Pages/Cars.aspx

Any idea why the test fails? I am expecting that it would match on the "www.mydomain.com" value.

Link to comment
Share on other sites

Sorry, I thought you want to compare strings exactly e.g. 'foo' = ('bar', 'foo', 'baz') is true as one item in right hand sequence is equal to the string on the left hand. The tokenize simply converts your original variable value into a sequence of strings and then when you compare with "=" the comparison is true if there is one item exactly equal to the string. You seem to want to do some pattern matching or maybe starts-with comparison like e.g.

<xsl:if test="tokenize($the_list, ',\s*')[starts-with($the_url, .)]">

Link to comment
Share on other sites

Sorry, I thought you want to compare strings exactly e.g. 'foo' = ('bar', 'foo', 'baz') is true as one item in right hand sequence is equal to the string on the left hand. The tokenize simply converts your original variable value into a sequence of strings and then when you compare with "=" the comparison is true if there is one item exactly equal to the string. You seem to want to do some pattern matching or maybe starts-with comparison like e.g.
<xsl:if test="tokenize($the_list, ',\s*')[starts-with($the_url, .)]">

Perfect, thanks!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...