Extract information from a JSON file using XSLT version 1.0

I am noobie for stackoverflow and xslt, so I hope that I do not look unreasonable!

So, I work with SDI for a GIS company, and I have a task that requires me to convert points that are in the same coordinate plane of the spatial coordinate system (SRS), such as EPSG: 4035, to the world of SRS, as well as EPSG : 4326. This is really not a problem for me, since I have the availability of an online service that will just give me what I want. However, the format it outputs is in JSON or HTML. For a while I was looking for a way to extract information from a JSON file, but most of the methods I saw use xslt: stylesheet version 2.0, and I have to use version 1.0. One of the methods that I thought about what I used was to use the xslt function ($ urlWithJsonFormat), however this only accepts xml files.

Here is an example of a formatted JSON file that I would get after a conversion request:

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

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

+4
source share
2 answers

You can use an external object to include JSON data as part of an XML file that you then transform.

For example, assuming JSON is saved as a file named "geometries.json", you can create an XML file as follows:

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

And then convert it with the following XSLT 1.0 style sheet:

 <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 create the following output:

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

The two main options here are (1) writing (or using) a JSON parser in XSLT 1.0 or (2) using a language other than XSLT. Since XSLT 1 engines usually cannot handle JSON directly, I would recommend using a different language for converting to XML.

https://github.com/WelcomWeb/JXS can help you if it is XSLT in a web browser.

+1
source

All Articles