使用 XSLT 1.0 版从 JSON 文件中提取信息 [英] extracting information from a JSON file using XSLT version 1.0

查看:25
本文介绍了使用 XSLT 1.0 版从 JSON 文件中提取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 stackoverflow 和 xslt 的菜鸟,所以我希望我不会听起来不聪明!

所以我正在与一家 GIS 公司的 SDI 合作,我有一项任务需要我将一个空间参考系统 (SRS) 坐标平面(例如 EPSG:4035)中的点转换为世界 SRS,也就是 EPSG:4326.这对我来说真的不是问题,因为我可以使用在线服务,它只会给我我想要的东西.但是,它输出的格式是 JSON 或 HTML.我浏览了一段时间以找到一种从 JSON 文件中提取信息的方法,但我见过的大多数技术都使用 xslt:stylesheet 2.0 版,而我必须使用 1.0 版.我考虑过的一种方法是使用 document($urlWithJsonFormat) xslt 函数,但这只接受 xml 文件.

以下是我要求转换后检索的 JSON 格式文件的示例:

<预>{几何":[{xmin":-4,ymin":-60,xmax":25,ymax":-41}]}

我只想要 xmin、ymin、xmax 和 ymax 值,仅此而已!这看起来很简单,但对我来说没有任何作用......

解决方案

您可以使用 外部实体 将 JSON 数据作为 XML 文件的一部分包含在内,然后您将对其进行转换.

例如,假设示例 JSON 被保存为名为geometries.json"的文件,您可以创建这样的 XML 文件:

<wrapper>&otherFile;</wrapper>

然后使用以下 XSLT 1.0 样式表对其进行转换:

<xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:template match="wrapper"><几何图形><xsl:call-template name="parse-json-member-value"><xsl:with-param name="member" select="'xmin'"/></xsl:call-template><xsl:call-template name="parse-json-member-value"><xsl:with-param name="member" select="'ymin'"/></xsl:call-template><xsl:call-template name="parse-json-member-value"><xsl:with-param name="member" select="'xmax'"/></xsl:call-template><xsl:call-template name="parse-json-member-value"><xsl:with-param name="member" select="'ymax'"/></xsl:call-template></几何图形></xsl:模板><xsl:template name="parse-json-member-value"><xsl:param name="member"/><xsl:element name="{$member}"><xsl:value-of select="normalize-space(翻译(子串之前(子串后(子串后(.,concat('"',$会员,'"')), ':'),'&#10;'), ',', ''))"/></xsl:element></xsl:模板></xsl:stylesheet>

产生以下输出:

<几何图形><xmin>-4</xmin><ymin>-60</ymin><xmax>25</xmax><ymax>-41</ymax></几何图形>

I'm a noobie to stackoverflow and xslt so I hope I don't sound unintelligent!

So I am working with SDI for a GIS company and I have a task that requires me to convert points that are in one spacial reference system (SRS) coordinate plane, such as EPSG:4035, to the world SRS, aka EPSG:4326. This really isn't a problem for me since I have the accessibility of an online service that will just give me what I want. However, the format that it outputs is in either JSON or HTML. I have browsed for a while to find a way to extract information from a JSON file but most of the techniques I have seen use xslt:stylesheet version 2.0, and I have to use version 1.0. One method I thought about doing was using the document($urlWithJsonFormat) xslt function, however this only accepts xml files.

Here is an example of the JSON formatted file that I would retrieve after asking for the conversion:

{
  "geometries" : 
  [{
      "xmin" : -4, 
      "ymin" : -60, 
      "xmax" : 25, 
      "ymax" : -41
    }
  ]
}

All I simply want are the xmin, ymin, xmax, and ymax values, that's all! It just seems so simple yet nothing works for me...

解决方案

You could use an external entity to include the JSON data as part of an XML file that you then transform.

For instance, assuming the example JSON is saved as a file called "geometries.json" you could create an XML file like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wrapper [
<!ENTITY otherFile SYSTEM "geometries.json">
]>
<wrapper>&otherFile;</wrapper>

And then transform it with the following XSLT 1.0 stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="wrapper">
    <geometries>
        <xsl:call-template name="parse-json-member-value">
            <xsl:with-param name="member" select="'xmin'"/>
        </xsl:call-template>
        <xsl:call-template name="parse-json-member-value">
            <xsl:with-param name="member" select="'ymin'"/>
        </xsl:call-template>
        <xsl:call-template name="parse-json-member-value">
            <xsl:with-param name="member" select="'xmax'"/>
        </xsl:call-template>
        <xsl:call-template name="parse-json-member-value">
            <xsl:with-param name="member" select="'ymax'"/>
        </xsl:call-template>
    </geometries>
</xsl:template>

    <xsl:template name="parse-json-member-value">
        <xsl:param name="member"/>
        <xsl:element name="{$member}">
            <xsl:value-of select="normalize-space(
                                    translate(
                                        substring-before(
                                            substring-after(
                                                substring-after(.,
                                                    concat('&quot;', 
                                                           $member, 
                                                          '&quot;'))
                                                , ':')
                                            ,'&#10;')
                                    , ',', '')
                                  )"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

To produce the following output:

<geometries>
   <xmin>-4</xmin>
   <ymin>-60</ymin>
   <xmax>25</xmax>
   <ymax>-41</ymax>
</geometries>

这篇关于使用 XSLT 1.0 版从 JSON 文件中提取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆