如何在XSL中用空格替换逗号

时间:2012-06-05 14:11:51

标签: xslt

我需要用XML输出中的空格替换所有其他逗号。现在,我的经度和经度看起来像这样:

-0.52437106918239,0.391509433962264,-0.533805031446541,0.430817610062893,0 
-0.547955974842767,0.427672955974843,

我需要XML输出中的坐标看起来像这样:

-0.52437106918239 0.391509433962264, -0.533805031446541 0.430817610062893,0   
-0.547955974842767 0.427672955974843

如何使用XSLT执行此操作?这是我的xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    
xmlns:kml="http://www.opengis.net/kml/2.2">
<xsl:output method="text"/>

<xsl:template match="/">
<xsl:apply-templates select="kml:kml/kml:Document/kml:Placemark/kml:Polygon  
/kml:outerBoundaryIs/kml:LinearRing"/>
</xsl:template>

  <xsl:template match="kml:LinearRing">
"POLYGON((<xsl:value-of select="kml:coordinates"/>))"
 </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

在XSLT 2.0中,它将是微不足道的。您可以使用replace()

在XSLT 1.0中,您可以使用这样的模板。在列表中调用需要每隔一秒逗号替换的转换空间模板。

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