我正在开发一个需要将 SQL 地理类型转换为 json 数组的项目。此时我选择的方法是使用 AsGML 选择 SQL 地理(给我一个 GML 表示)。
我正在使用 xslt 将生成的 GML 转换为我需要的 json 格式。
我不是 xslt 的专家——这可能是一个简单的问题。
我现在遇到的问题是 posList 元素的返回有一个尾随逗号,我无法从最终结果中删除它。
在此先感谢您的帮助 - 示例代码如下:
XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="MultiSurface">
<MultiGeometry>
<xsl:apply-templates />
</MultiGeometry>
</xsl:template>
<xsl:template match="Polygon">
<Polygon>
<xsl:apply-templates />
</Polygon>
</xsl:template>
<xsl:template match="Point">
<Point>
<xsl:apply-templates />
</Point>
</xsl:template>
<xsl:template match="exterior">
{"ringtype":"exterior",
<xsl:apply-templates />
},
</xsl:template>
<xsl:template match="interior">
{"ringtype":"interior",
<xsl:apply-templates />
},
</xsl:template>
<xsl:template match="posList">
"coordinates":[
<xsl:call-template name="split">
<xsl:with-param name="str" select="normalize-space(.)" />
</xsl:call-template>
]
</xsl:template>
<xsl:template match="pos">
<coordinates>
<xsl:call-template name="split">
<xsl:with-param name="str" select="normalize-space(.)" />
</xsl:call-template>
</coordinates>
</xsl:template>
<xsl:template name="split">
<xsl:param name="str" />
<xsl:choose>
<xsl:when test="contains($str,'' '')">
<xsl:variable name="first">
<xsl:value-of select="format-number(number(substring-before($str,'' '')),''00.000000'')" />
</xsl:variable>
<xsl:variable name="second">
<xsl:value-of select="format-number(number(substring-before(substring-after(concat($str,'' ''),'' ''),'' '')),''00.000000'')" />
</xsl:variable>
<xsl:value-of select="concat(''['',$first,'','',$second,''],'')" />
<xsl:call-template name="split">
<xsl:with-param name="str">
<xsl:value-of select="substring-after(substring-after($str,'' ''),'' '')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
输入示例:
<Polygon xmlns="http://www.opengis.net/gml">
<exterior>
<LinearRing>
<posList>32.230546755000034 -95.316506964999917 32.230542547000084 -95.316051441999946</posList>
</LinearRing>
</exterior>
</Polygon>
输出示例(注意坐标元素中的尾随逗号)
<Polygon>
{"ringtype":"exterior",
"coordinates":[
[32.230547,-95.316507],[32.230543,-95.316051],[32.230536,-95.315358],
]
},
</Polygon>