使用XSLT嵌入JSON的coordinates数组到HTML时格式缺失问题
解决XSLT输出JSON坐标数组缺少括号和逗号的问题
你遇到的问题是因为<xsl:value-of>仅会输出JSON数组元素的空格分隔字符串值,而非标准的JSON数组格式(带方括号和逗号)。要正确生成符合GeoJSON规范的坐标结构,需使用XSLT 3.0的serialize()函数对数组进行JSON序列化。
修正后的代码
<xsl:variable name="test" select="json-doc('https://deims.org/api/locations/4930521a-596f-4e6f-90b8-29696b8c1c98')"/> <script type="application/json"> { "type":"Feature", "geometry":{ "type": "Polygon", "coordinates": <xsl:value-of select="serialize($test?geometry?coordinates, map{'method':'json'})"/> } } </script>
核心改动说明
- 将
xsl:variable声明移至JSON内容块外部,修复原代码的语法错误。 serialize($test?geometry?coordinates, map{'method':'json'})会将坐标数组序列化为标准JSON格式,自动添加方括号和元素间的逗号,生成符合GeoJSON要求的嵌套数组结构(例如[[8.61638,45.962042, ...]])。
内容的提问来源于stack exchange,提问作者Alessandro Oggioni
相关产品推荐
相关产品推荐

