|
| 1 | +<?xml version='1.0' ?> |
| 2 | +<xsl:stylesheet |
| 3 | + xmlns:xsl='http://www.w3.org/1999/XSL/Transform' |
| 4 | + xmlns:x="http://www.ibm.com/xmlns/prod/2009/jsonx" |
| 5 | + version='1.0' |
| 6 | + > |
| 7 | +<!-- |
| 8 | +
|
| 9 | +Author: |
| 10 | + Pierre Lindenbaum PhD |
| 11 | + plindenbaum@yahoo.fr |
| 12 | +
|
| 13 | +Motivation: |
| 14 | + JSONX to gjson |
| 15 | +Usage: |
| 16 | + xsltproc x |
| 17 | +--> |
| 18 | +<xsl:output method="text" /> |
| 19 | + |
| 20 | + |
| 21 | +<xsl:template match="/"> |
| 22 | +<xsl:apply-templates select="*" mode="leaf"/> |
| 23 | +</xsl:template> |
| 24 | + |
| 25 | +<xsl:template match="*" mode="leaf"> |
| 26 | +<xsl:choose> |
| 27 | + <xsl:when test="count(*) != 0"> |
| 28 | + <xsl:apply-templates select="*" mode="leaf"/> |
| 29 | + </xsl:when> |
| 30 | + <xsl:otherwise> |
| 31 | + <xsl:apply-templates select="." mode="up0"/> |
| 32 | + </xsl:otherwise> |
| 33 | +</xsl:choose> |
| 34 | +</xsl:template> |
| 35 | + |
| 36 | + |
| 37 | +<xsl:template match="*" mode="allup"> |
| 38 | +<xsl:if test="parent::*"> |
| 39 | + <xsl:apply-templates select="parent::*" mode="up"/> |
| 40 | +</xsl:if> |
| 41 | +<xsl:choose> |
| 42 | + <xsl:when test="@name">if(o==null || !o.isJsonObject() || !o.getAsJsonObject().has("<xsl:value-of select="@name"/>")) return null; |
| 43 | +o = o.getAsJsonObject().get("<xsl:value-of select="@name"/>"); |
| 44 | +</xsl:when> |
| 45 | + <xsl:when test="local-name(..)='array'">if(o==null || !o.isJsonArray() || o.getAsJsonArray().size() <= <xsl:value-of select="count(preceding-sibling::*)"/> ) return null; |
| 46 | +o = o.getAsJsonArray().get(<xsl:value-of select="count(preceding-sibling::*)"/>); |
| 47 | +</xsl:when> |
| 48 | +</xsl:choose> |
| 49 | +</xsl:template> |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +<xsl:template match="x:object" mode="up"> |
| 54 | +<xsl:apply-templates select="." mode="allup"/> |
| 55 | +if(o==null || !o.isJsonObject()) return null; |
| 56 | +</xsl:template> |
| 57 | + |
| 58 | +<xsl:template match="x:array" mode="up"> |
| 59 | +<xsl:apply-templates select="." mode="allup"/> |
| 60 | +if(o==null || !o.isJsonArray()) return null; |
| 61 | +</xsl:template> |
| 62 | + |
| 63 | +<xsl:template match="x:string" mode="up"> |
| 64 | +<xsl:apply-templates select="." mode="allup"/> |
| 65 | +if(o==null || !o.isJsonPrimitive() || !o.getAsJsonPrimitive().isString()) return null; |
| 66 | +</xsl:template> |
| 67 | + |
| 68 | +<xsl:template match="x:number" mode="up"> |
| 69 | +<xsl:apply-templates select="." mode="allup"/> |
| 70 | +if(o==null || !o.isJsonPrimitive() || !o.getAsJsonPrimitive().isNumber()) return null; |
| 71 | +</xsl:template> |
| 72 | + |
| 73 | +<xsl:template match="x:boolean" mode="up"> |
| 74 | +<xsl:apply-templates select="." mode="allup"/> |
| 75 | +if(o==null || !o.isJsonPrimitive() || !o.getAsJsonPrimitive().isBoolean()) return null; |
| 76 | +</xsl:template> |
| 77 | + |
| 78 | + |
| 79 | +<xsl:template match="x:null" mode="up"> |
| 80 | +<xsl:apply-templates select="." mode="allup"/> |
| 81 | +if(o==null || !o.isJsonNull()) return null; |
| 82 | +</xsl:template> |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +<xsl:template match="x:object" mode="up0"> |
| 87 | +public JsonObject get() { |
| 88 | +JsonElement o = this.root; |
| 89 | +<xsl:apply-templates select="." mode="allup"/> |
| 90 | +if(o==null || !o.isJsonObject()) return null; |
| 91 | +return o.getAsJsonObject(); |
| 92 | +}; |
| 93 | +</xsl:template> |
| 94 | + |
| 95 | +<xsl:template match="x:array" mode="up0"> |
| 96 | +public JsonArray get() { |
| 97 | +JsonElement o = this.root; |
| 98 | +<xsl:apply-templates select="." mode="allup"/> |
| 99 | +if(o==null || !o.isJsonArray()) return null; |
| 100 | +return o.getAsJsonArray(); |
| 101 | +}; |
| 102 | +</xsl:template> |
| 103 | + |
| 104 | +<xsl:template match="x:string" mode="up0"> |
| 105 | +public String get() { |
| 106 | +JsonElement o = this.root; |
| 107 | +<xsl:apply-templates select="." mode="allup"/> |
| 108 | +if(o==null || !o.isJsonPrimitive() || !o.getAsJsonPrimitive().isString()) return null; |
| 109 | +return o.getAsJsonPrimitive().getAsString(); /* <xsl:value-of select="text()"/> */ |
| 110 | +}; |
| 111 | +</xsl:template> |
| 112 | + |
| 113 | +<xsl:template match="x:number" mode="up0"> |
| 114 | +public Number get() { |
| 115 | +JsonElement o = this.root; |
| 116 | +<xsl:apply-templates select="." mode="allup"/> |
| 117 | +if(o==null || !o.isJsonPrimitive() || !o.getAsJsonPrimitive().isNumber()) return null; |
| 118 | +return o.getAsJsonPrimitive().getAsNumber(); /* <xsl:value-of select="text()"/> */ |
| 119 | +}; |
| 120 | +</xsl:template> |
| 121 | + |
| 122 | +<xsl:template match="x:boolean" mode="up0"> |
| 123 | +public Boolean get() { |
| 124 | +JsonElement o = this.root; |
| 125 | +<xsl:apply-templates select="." mode="allup"/> |
| 126 | +if(o==null || !o.isJsonPrimitive() || !o.getAsJsonPrimitive().isBoolean()) return null; |
| 127 | +return o.getAsJsonPrimitive().getAsBoolean(); /* <xsl:value-of select="text()"/> */ |
| 128 | +}; |
| 129 | +</xsl:template> |
| 130 | + |
| 131 | + |
| 132 | +<xsl:template match="x:null" mode="up0"> |
| 133 | +public JsonNull get() { |
| 134 | +JsonElement o = this.root; |
| 135 | +<xsl:apply-templates select="." mode="allup"/> |
| 136 | +return o.getAsJsonNull(); |
| 137 | +} |
| 138 | +</xsl:template> |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +</xsl:stylesheet> |
0 commit comments