-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmpxmlresult2simpleJSON.xslt
78 lines (78 loc) · 3.12 KB
/
fmpxmlresult2simpleJSON.xslt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?xml version="1.0" encoding="UTF-8"?>
<!--
Written by Gjermund G Thorsen 2018, all rights deserved
for the purpose of generating JSON from FMPXMLRESULT
as simple as possible, as complicated as necessary to bring the data back in to the same FileMaker table
No support for repeating fields for now.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fmp="http://www.filemaker.com/fmpxmlresult" version="1.0">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="fmp:FMPXMLRESULT">
<xsl:text>{"</xsl:text><xsl:value-of select="$tableName"/><xsl:text>":{</xsl:text>
<xsl:call-template name="fieldname"/>
<xsl:call-template name="data"/>
<xsl:text>}}</xsl:text>
</xsl:template>
<xsl:template name="fieldname">
<xsl:text>"field":[</xsl:text>
<xsl:for-each select="fmp:METADATA/fmp:FIELD">
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:text>"</xsl:text><xsl:value-of select="@NAME"/><xsl:text>"</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>"</xsl:text><xsl:value-of select="@NAME"/><xsl:text>",</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:text>],</xsl:text>
</xsl:template>
<xsl:template name="data">
<xsl:text>"data":{</xsl:text>
<xsl:for-each select="fmp:RESULTSET/fmp:ROW">
<xsl:text>"</xsl:text><xsl:value-of select="@RECORDID"/><xsl:text>.</xsl:text><xsl:value-of select="@MODID"/><xsl:text>":[</xsl:text>
<xsl:for-each select="fmp:COL">
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:text>"</xsl:text><xsl:value-of select="fmp:DATA"/><xsl:text>"</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>"</xsl:text><xsl:value-of select="fmp:DATA"/><xsl:text>",</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:text>]</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>],</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:text>}</xsl:text>
</xsl:template>
<!--
Todo
; [ "\\" ; "\\\\" ]
; [ Char(11) ; "\n" ] /* new line within field */
; [ Char(9) ; "\t" ] /*tab */
; [ Char(29) ; "#" ] /* repeating field separator */
; [ "\"\"" ; "\\\"" ] /* quoted values */
-->
<xsl:variable name="databaseName">
<xsl:value-of select="fmp:FMPXMLRESULT/fmp:DATABASE/@NAME"/>
</xsl:variable>
<xsl:variable name="tableName">
<xsl:value-of select="fmp:FMPXMLRESULT/fmp:DATABASE/@LAYOUT"/>
</xsl:variable>
<xsl:variable name="timeformat">
<xsl:value-of select="fmp:FMPXMLRESULT/fmp:DATABASE/@TIMEFORMAT"/>
</xsl:variable>
</xsl:stylesheet><!--
========================================================================================
Copyright (c) 2008 Gjermund Gusland Thorsen, released under the MIT License.
All rights deserved.
This piece comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
========================================================================================
-->