如何在XSL空格代替逗号(How to replace commas with spaces in

2019-06-25 05:30发布

我需要在我的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>

Answer 1:

在XSLT 2.0这将是微不足道的。 你可以使用替换() 。

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


文章来源: How to replace commas with spaces in XSL
标签: xslt