2

我正在开发一个需要将 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>
4

1 回答 1

3

我已将您的 xslt(并且有很多更正)从无法编译的 xslt 更正为不会产生多余逗号的 xslt :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://www.opengis.net/gml" exclude-result-prefixes="x">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

    <xsl:template match="x:MultiSurface">
        <MultiGeometry>
            <xsl:apply-templates />
        </MultiGeometry>
    </xsl:template>

    <xsl:template match="x:Polygon">
        <Polygon>
            <xsl:apply-templates />
        </Polygon>
    </xsl:template>

    <xsl:template match="x:Point">
        <Point>
            <xsl:apply-templates />
        </Point>
    </xsl:template>

    <xsl:template match="x:exterior">
    <xsl:if test="preceding-sibling::exterior">,</xsl:if>
    {"ringtype":"exterior",
        <xsl:apply-templates />
    }
    </xsl:template>

    <xsl:template match="x:interior">
    {"ringtype":"interior",
        <xsl:apply-templates />
    },
    </xsl:template>

    <xsl:template match="x:posList">
    "coordinates":[
        <xsl:call-template name="split">
            <xsl:with-param name="str" select="normalize-space(.)" />
        </xsl:call-template>
    ]
    </xsl:template>

    <xsl:template match="x: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:if test="substring-after(substring-after($str,' '),' ')">
                 <xsl:text>, </xsl:text>
                    <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:if>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

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

    }
    </Polygon>
于 2012-08-05T21:12:45.497 回答