在模板中使用 2 XSLT 子字符串函数

Using 2 XSLT substring function in template

为什么我不能在模板中使用这个 XSLT 字符串函数?

<xsl:with-param name="text" select="substring($text,'2') and substring($text,1,(string-length($text)-1))" />

这是模板:

<!-- Template to remove double quotes if available in first and last position of any field -->
  <xsl:template name="remove-quotes">
    <xsl:param name="text"/>
   <xsl:param name="quot" select="'&quot;'"/>
   <xsl:param name="trim1" select="substring($text,'2')"/>
   <xsl:param name="trim2" select="substring($text,1,(string-length($text)-1))"/>
  <xsl:choose>
  <xsl:when test="starts-with($text,$quot) and ends-with($text,$quot)">
        <xsl:call-template name="remove-quotes">
      <xsl:with-param name="text" select="$trim1 and $trim2"/> 
    </xsl:call-template>
    </xsl:when>
  <xsl:otherwise>
        <xsl:value-of select="$text"/>
    </xsl:otherwise>
    </xsl:choose>
   </xsl:template>

调用者:

<xsl:call-template name="remove-quotes">
 <xsl:with-param name="text" select="XXXXX"/>
</xsl:call-template>

我不确定你的模板试图做什么,但肯定这部分没有意义:

<xsl:call-template name="remove-quotes">
    <xsl:with-param name="text" select="$trim1 and $trim2"/> 
</xsl:call-template>

and 是布尔运算符。包含 and returns 的表达式 true()false().

与以下相同:

<xsl:with-param name="text" select="substring($text,'2') and substring($text,1,(string-length($text)-1))" />

已添加:

要删除前导引号或尾随引号或两者,您只需执行以下操作:

<xsl:variable name="lead" select="number(starts-with($text, '&quot;'))" />
<xsl:variable name="trail" select="number(ends-with($text, '&quot;'))" />
<xsl:value-of select="substring($text, 1 + $lead, string-length($text) - $lead - $trail)" />

XSLT 模板:

    <!-- Template to remove trailing and leading double quotes from the fields -->
     <xsl:template name="remove-quotes">
    <xsl:param name="text"/>
    <xsl:param name="quot" select="'&quot;'"/>
    <xsl:param name="lead" select="number(starts-with($text, '&quot;'))"/>
    <xsl:param name="trail" select="number(ends-with($text, '&quot;'))"/>
    <xsl:choose>
    <xsl:when test="starts-with($text,$quot) and ends-with($text,$quot)">
        <xsl:call-template name="remove-quotes">
      <xsl:with-param name="text" select="substring($text, 1 + $lead, string-length($text) - $lead - $trail)"/> 
    </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$text"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>

这样调用:

    <xsl:call-template name="remove-quotes">
                <xsl:with-param name="text" select="SampleText"/>
    </xsl:call-template>