XSLT - 检查子字符串

135*_*355 4 xslt substring

我有两个XSLT变量,如下所示:

<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'" /> 
Run Code Online (Sandbox Code Playgroud)

如何检查第二个字符串(dynamicUrl)是否是第一个字符串(staticBaseUrl)的子字符串?

dog*_*ane 19

要检查一个字符串是否包含在另一个字符串中,请使用该contains函数.

例:

  <xsl:if test="contains($staticBaseUrl,$dynamicUrl)">
    <xsl:text>Yes!</xsl:text>
  </xsl:if>
Run Code Online (Sandbox Code Playgroud)

更新:

对于不区分大小写的包含,您需要在调用之前首先将两个字符串转换为相同的大小写contains.在XSLT 2.0中,您可以使用该upper-case函数,但在XSLT 1.0中,您可以使用以下命令:

<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>
Run Code Online (Sandbox Code Playgroud)

  • @1355 尝试使用 `contains(upper-case($staticBaseUrl), upper-case($dynamicUrl))`。这仅在您使用 xslt2.0 时有效。 (2认同)