DigitalMC
DigitalMC

Reputation: 877

Check text string against an array in if-test

Is there any way to check if a text string contains any of the entries in pre-determined array? The use case is I am parsing through a large amount of text and looking for text links. As a result I am scrubbing through each word and doing checks on each word looking for popular top level domains to see if they are a link. Here is some broken code but gives you an idea:

XML:

<feed>
  <description>You can come here to yourwebsite.org to learn more</description>
</feed>

XSL:

<xsl:variable name="tlds" select="'.com .net .org .edu .gov .ly'" />

*** There is a bunch of XSL here that chopps up description into individual words to check ***

<xsl:if test="contains($current_word, $tlds)">
  The current word being checked contained an item in the array! 
</xsl:if>

Everything is working except the check for the top level domains. So I don't need any loops or anything, just how to do the check against the $tlds array.

Upvotes: 0

Views: 7289

Answers (2)

hr_117
hr_117

Reputation: 9627

Here a solution if your variable $tlds has to be an "array" (blank separated list of identifier). The idea is to split this list in words withe recursive template calls. (Probably as you already do with your input text.)

<xsl:template name="checkword">
    <xsl:param name="current_word"/>
    <xsl:variable name="contain_tld">
        <xsl:call-template name="check_tlds">
            <xsl:with-param name="current_word" select="$current_word"/>
            <xsl:with-param name="l_tlds" select="$tlds"/>
            <xsl:with-param name="cnt_tlds" select="0"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:if test="$contain_tld &gt; 0">
    The current word being checked contained an item in the array!
    </xsl:if>

</xsl:template>

<xsl:template name="check_tlds">
    <xsl:param name="current_word"/>
    <xsl:param name="l_tlds"/>
    <xsl:param name="cnt_tlds"/>

    <xsl:choose>
        <xsl:when test="contains($l_tlds,' ')">
            <xsl:variable name="result">
                <xsl:call-template name="check_tlds">
                    <xsl:with-param name="current_word" select="$current_word"/>
                    <xsl:with-param name="l_tlds" select="substring-before($l_tlds,' ')"/>
                    <xsl:with-param name="cnt_tlds" select="0"/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="check_tlds">
                <xsl:with-param name="current_word" select="$current_word"/>
                <xsl:with-param name="l_tlds" select="substring-after($l_tlds,' ')"/>
                <xsl:with-param name="cnt_tlds" select="$cnt_tlds+ $result"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:choose>
                <xsl:when test="contains( $current_word, $l_tlds)">
                    <xsl:value-of select="$cnt_tlds + 1"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$cnt_tlds"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

This only should show the idea. There certainly some enhancements possible.

Upvotes: 2

Tim C
Tim C

Reputation: 70638

At the moment, your $tlds variable is not an array, but a simple string. What you could do is set the variable like this to make it more like an array

<xsl:variable name="tlds">
   <tld>.com</tld>
   <tld>.net</tld>
   <tld>.org</tld>
   <tld>.edu</tld>
   <tld>.ly</tld>
</xsl:variable>

Then define another variable to reference this variable in the XSLT stylesheet

<xsl:variable name="lookup" select="document('')//xsl:variable[@name='tlds']"/>

Then, to look up if a word exists in the $tlds variable, just do this:

   <xsl:if test="$lookup/tld=$word">
       The current word being checked contained an item in the array!
   </xsl:test>

EDIT: Or, if you want to check if a word contains one of the items in the array, you would do this:

   <xsl:if test="$lookup/tld[contains($word, .)]">
       The current word being checked contained an item in the array!
   </xsl:test>

For example, here is some XSLT to show this fully in action

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>

   <xsl:variable name="tlds">
      <tld>.com</tld>
      <tld>.net</tld>
      <tld>.org</tld>
      <tld>.edu</tld>
      <tld>.ly</tld>
   </xsl:variable>

   <xsl:variable name="lookup" select="document('')//xsl:variable[@name='tlds']"/>

   <xsl:template match="description">
      <xsl:call-template name="checkword">
         <xsl:with-param name="word">www.pie.com</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="checkword">
         <xsl:with-param name="word">www.pie.co.uk</xsl:with-param>
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="checkword">
      <xsl:param name="word"/>
      <xsl:choose>
         <xsl:when test="$lookup/tld[contains($word, .)]">
             <xsl:value-of select="$word" /> is contained an item in the array!
         </xsl:when>
         <xsl:otherwise>
             <xsl:value-of select="$word" /> is not in the array
           </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

This should output the following:

www.pie.com is contained an item in the array!
www.pie.co.uk is not in the array

Upvotes: 2

Related Questions