Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

extracting information from a JSON file using XSLT version 1.0

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...