检查文本字符串反对,如果测试一个数组 [英] Check text string against an array in if-test

查看:101
本文介绍了检查文本字符串反对,如果测试一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法来检查,如果文本字符串中包含任何在pre-确定数组中的条目?用例是我通过大量的文本分析和寻找文本链接。这样一来,我通过每个字擦洗并在每个单词寻找流行的顶级域名,看看他们是否有一个链接做检查。下面是一些破碎code,但给你一个想法:

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.

推荐答案

下面,如果你的变量$顶级域名必须是一个阵列(标识的空白分隔列表)的解决方案。
我们的想法是用文字枝条递归调用模板分裂这个名单。
(可能是因为你已经与你的输入文本。)

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.

这篇关于检查文本字符串反对,如果测试一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆