如何在 XSL 中用空格替换逗号 [英] How to replace commas with spaces in XSL

查看:28
本文介绍了如何在 XSL 中用空格替换逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 XML 输出中用空格替换所有其他逗号.现在,我的纬度和经度如下所示:

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.391509433962264,-0.533805031446541,0.430817610062893,0 
-0.547955974842767,0.427672955974843,

我需要 XML 输出中的坐标如下所示:

I need the coordinates in my XML output to look like this:

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

我如何使用 XSLT 来做到这一点?这是我的 xsl:

How can I use XSLT to do this? Here is my 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 2.0 中,这将是微不足道的.您可以使用 replace().

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

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

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>

这篇关于如何在 XSL 中用空格替换逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆