How to replace commas with spaces in XSL

后端 未结 1 396
我在风中等你
我在风中等你 2021-01-17 01:09

I need to replace every other comma with a space in my XML output. Right now, I have latitude and longitude that looks like this:

-0.52437106918239,0.3915094         


        
相关标签:
1条回答
  • 2021-01-17 01:53

    In XSLT 2.0 it would be trivial. You could use replace().

    In XSLT 1.0, you could use template like so. Call the convert-space template on your list that needs every second comma replaced.

    <xsl:template name="convert-space">
      <xsl:param name="text"/>
      <xsl:choose>
        <xsl:when test="contains($text,',')">
          <xsl:value-of select="substring-before($text,',')"/>
          <xsl:value-of select="' '"/>
          <xsl:call-template name="convert-comma">
            <xsl:with-param name="text" select="substring-after($text,',')"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$text"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    
    <xsl:template name="convert-comma">
      <xsl:param name="text"/>
      <xsl:choose>
        <xsl:when test="contains($text,',')">
          <xsl:value-of select="substring-before($text,',')"/>
          <xsl:value-of select="','"/>
          <xsl:call-template name="convert-space">
            <xsl:with-param name="text" select="substring-after($text,',')"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$text"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    
    0 讨论(0)
提交回复
热议问题