我有一个以空格分隔的坐标元组列表,其中包含任意多个元组。每个元组由一个以空格分隔的二维坐标列表组成。例如,“1.1 2.8 1.2 2.9”表示从 POINT(1.1 2.8) 到 POINT(1.2 2.9) 的一条线。我需要这个改为“1.1,2.8 1.2,2.9”。我将如何使用 XSLT 替换数字对之间的空格到逗号?我有“字符串(gml:LinearRing/gml:posList)”。
这被用在一个 Java Web 服务上,该服务使用几何图形输出 GML 3.1.1 特性。该服务支持可选的 KML 输出,通过使用 XSLT 将 GML 文档转换为 KML 文档(至少,被认为“重要”的块)。我被锁定在 XSLT 1.0 中,因此不能选择 XSLT 2.0 中的正则表达式。
我知道 GML 使用纬度/经度,而 KML 使用经度/纬度。这是在 XSLT 之前处理的,但如果用 XSLT 也能完成它会很好。
感谢您的解决方案,Dimitre。我对其进行了一些修改以满足我的需要,因此我将在此处包含它以防万一它对其他人有所帮助。假设二维元组,它通过坐标列表执行递归。
这执行两个功能:轴交换(纬度/经度为经度/纬度,根据 GML 和 KML 规范)和将每个元组内的坐标分隔符从空格 ' ' 更改为逗号 ','。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gml="http://www.opengis.net/gml" exclude-result-prefixes="gml">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<!-- other portions omitted -->
<xsl:template match="gml:pos">
<xsl:call-template name="coordinateSequence">
<xsl:with-param name="coords" select="normalize-space(string(.))" />
</xsl:call-template>
</xsl:template>
<xsl:template match="gml:posList">
<xsl:call-template name="coordinateSequence">
<xsl:with-param name="coords" select="normalize-space(string(.))" />
</xsl:call-template>
</xsl:template>
<xsl:template name="coordinateSequence">
<xsl:param name="coords" />
<xsl:if test="string-length($coords) > 0">
<xsl:variable name="lat" select="substring-before($coords, ' ')" />
<xsl:variable name="lon">
<xsl:value-of select="substring-before(substring-after($coords, ' '), ' ')" />
<xsl:if test="string-length(substring-before(substring-after($coords, ' '), ' ')) = 0">
<xsl:value-of select="substring-after($coords, ' ')" />
</xsl:if>
</xsl:variable>
<xsl:variable name="remainder" select="substring-after(substring-after($coords, ' '), ' ')" />
<xsl:value-of select="concat($lon, ',', $lat)" />
<xsl:if test="string-length($remainder) > 0">
<xsl:value-of select="' '" />
<xsl:call-template name="coordinateSequence">
<xsl:with-param name="coords" select="$remainder" />
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>