简体   繁体   中英

Using 2 XSLT substring function in template

Why cant I use this XSLT string function in a template?

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

Here is the template:

<!-- 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>

Invoked by:

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

I am not sure what your template is trying to do, but certainly this part makes no sense:

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

and is Boolean operator. An expression that contains and returns a result of either true() or false() .

Same thing with:

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

Added:

To remove either a leading or a trailing quote or both, you could do simply:

<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:

    <!-- 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>

Invoke like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM