【问题标题】:How to replace commas with spaces in XSL如何在 XSL 中用空格替换逗号
【发布时间】:2012-06-05 14:11:51
【问题描述】:

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

【问题讨论】:

    标签: xslt


    【解决方案1】:

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

    在 XSLT 1.0 中,您可以像这样使用模板。调用列表中需要每隔一个逗号替换的 convert-space 模板。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-13
      • 2014-04-17
      • 2017-11-18
      • 1970-01-01
      • 2019-04-28
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多