-
Notifications
You must be signed in to change notification settings - Fork 0
/
FMPXMLRESULT2psqlCOPY.xslt
84 lines (82 loc) · 2.59 KB
/
FMPXMLRESULT2psqlCOPY.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
79
80
81
82
83
84
<?xml version="1.0" encoding="UTF-8"?>
<!-- Written by Gjermund G Thorsen -->
<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>CREATE TABLE "</xsl:text>
<xsl:value-of select="$databaseName"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="$tableName"/>
<xsl:text>" (
</xsl:text>
<xsl:for-each select="fmp:METADATA/fmp:FIELD">
<xsl:text>"</xsl:text>
<xsl:value-of select="@NAME"/>
<xsl:text>"</xsl:text>
<xsl:text> </xsl:text>
<xsl:value-of select="concat(
substring( 'text', 1 div boolean( @TYPE = 'TEXT' ) ),
substring( 'blob', 1 div boolean( @TYPE = 'CONTAINER' ) ),
substring( 'date', 1 div boolean( @TYPE = 'DATE' ) ),
substring( 'timestamp', 1 div boolean( @TYPE = 'TIMESTAMP' ) ),
substring( 'numeric', 1 div boolean( @TYPE = 'NUMBER' ) )
)"/>
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<xsl:text>,
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:text>
);
</xsl:text>
<xsl:text>COPY "</xsl:text>
<xsl:value-of select="$databaseName"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="$tableName"/>
<xsl:text>" ( </xsl:text>
<xsl:for-each select="fmp:METADATA/fmp:FIELD">
<xsl:text>"</xsl:text>
<xsl:value-of select="@NAME"/>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<xsl:text>, </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each><xsl:text> ) FROM stdin;
</xsl:text>
<xsl:for-each select="fmp:RESULTSET/fmp:ROW">
<xsl:for-each select="fmp:COL">
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:value-of select="fmp:DATA"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="fmp:DATA"/><xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:text>\.
</xsl:text>
</xsl:template>
<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>