XSLT - Checking for a substring

2020-06-23 07:00发布

I have two XSLT variables as given below:

<xsl:variable name="staticBaseUrl" select="'https://www.hello.com/htapi/PrintApp.asmx/getGames?contentId=id_sudoku&uniqueId="123456"&pageformat=a4'" /> 

<xsl:variable name="dynamicUrl" select="'https://www.hello.com/htapi/PrintApp.asmx/getGames'" /> 

How to check whether the second string (dynamicUrl) is a substring of the first string (staticBaseUrl) or not?

1条回答
我只想做你的唯一
2楼-- · 2020-06-23 07:26

To check if one string is contained in another, use the contains function.

Example:

  <xsl:if test="contains($staticBaseUrl,$dynamicUrl)">
    <xsl:text>Yes!</xsl:text>
  </xsl:if>

Update:

For case-insensitive contains, you need to first convert the two strings to the same case before calling contains. In XSLT 2.0 you can use the upper-case function, but in XSLT 1.0 you can use the following:

<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

<xsl:template match="/">
    <xsl:if
        test="contains(translate($staticBaseUrl,$smallcase,$uppercase), translate($dynamicUrl,$smallcase,$uppercase))">
        <xsl:text>Yes!</xsl:text>
    </xsl:if>
</xsl:template>
查看更多
登录 后发表回答