Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT - Checking for a substring

Tags:

substring

xslt

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?

like image 328
Sarika.S Avatar asked Nov 29 '22 04:11

Sarika.S


1 Answers

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>
like image 100
dogbane Avatar answered Dec 05 '22 02:12

dogbane