我有一个以空格分隔的坐标元组列表,其中包含任意多个元组。每个元组由空格分隔的二维坐标列表组成。例如。 “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 文档(至少是被认为“重要”的 block )。我被锁定在 XSLT 1.0 中,因此 XSLT 2.0 中的正则表达式不是一个选项。
我知道 GML 使用纬度/经度,而 KML 使用经度/纬度。这是在 XSLT 之前处理的,不过如果也使用 XSLT 来处理就好了。
感谢您的解决方案,Dimitre。我对其进行了一些修改以满足我的需要,因此我将其包含在此处,以防对其他人有所帮助。它通过坐标列表执行递归,假设是二维元组。
这执行两个功能:轴交换(lat/lon 为 lon/lat,根据 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>
最佳答案
此转换(还显示中间步骤):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:variable name="vNorm" select="normalize-space()"/>
<xsl:variable name="vP1" select=
"concat(substring-before(., ' '), ',',
substring-before(substring-after($vNorm, ' '),' ')
)"/>
<xsl:variable name="vPart2" select="substring-after(substring-after($vNorm,' '),' ')"/>
<xsl:variable name="vP2" select=
"concat(substring-before($vPart2, ' '), ',',
substring-after($vPart2, ' ')
)"/>
<xsl:value-of select="$vP1"/>
==========
<xsl:value-of select="$vP2"/>
==========
<xsl:value-of select="concat($vP1, ' ', $vP2)"/>
</xsl:template>
</xsl:stylesheet>
应用于此 XML 文档时:
<t>1.1 2.8 1.2 2.9</t>
产生想要的正确结果(最后一行):
1.1,2.8
==========
1.2,2.9
==========
1.1,2.8 1.2,2.9
为了方便起见,可以将此代码放入命名模板中,以便为每个想要的行调用:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:call-template name="convertLine"/>
</xsl:template>
<xsl:template name="convertLine">
<xsl:param name="pStr" select="."/>
<xsl:variable name="vNorm" select="normalize-space($pStr)"/>
<xsl:variable name="vP1" select=
"concat(substring-before($pStr, ' '), ',',
substring-before(substring-after($vNorm, ' '),' ')
)"/>
<xsl:variable name="vPart2" select="substring-after(substring-after($vNorm,' '),' ')"/>
<xsl:variable name="vP2" select=
"concat(substring-before($vPart2, ' '), ',',
substring-after($vPart2, ' ')
)"/>
<xsl:value-of select="concat($vP1, ' ', $vP2)"/>
</xsl:template>
</xsl:stylesheet>
关于xslt - 如何使用 XSLT 将坐标分隔符替换为元组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12503270/