java


convert JSON to XML using XSLT 3 in java


When i was trying to transform JSON with XSLT to XML I am not able to transform, it is throwing the prolog exception, when i put the Json in tags i am able to transform. Can any one give an example how to transform from Json to XML with XSLT3.0.
Here's one example using json-to-xml(). It processes all json files in a specified directory (collection param).
I've also used json-doc() with uri-collection() and map:get() to process very simple json files.
JSON (File "so.json" based on the JSON in the question How to convert json to xml using xslt)
{"Order": {
"InvestmentAccount": {"AccountNumber": "10"},
"Parcel": {
"Limit": "0",
"ExpiryDate": "1900-01-01T00:00:00",
"Asset": [
{
"Open": "25.15",
"High": "25.15",
"Low": "25.11",
"Close": "25.87"
},
{
"Open": "25.15",
"High": "25.15",
"Low": "25.11",
"Close": "25.87"
}
]
},
"OrderDate": "2012-10-11T21:46:03.6489906+11:00"
}}
XSLT 3.0 (Tested using Saxon-HE 9.7.0.8J from the command line specifying init as the initial template.)
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs map">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="collection" select="'file:///C:/some/dir'"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="init">
<xsl:for-each select="uri-collection(concat($collection,'?select=*.json'))">
<xsl:variable name="uri" select="."/>
<xsl:variable name="filename" select="tokenize($uri,'/')[last()]"/>
<xsl:message>Processing json file: <xsl:value-of select="$filename"/>...</xsl:message>
<xsl:variable name="json" select="unparsed-text($uri)"/>
<xsl:result-document href="{substring-before($filename,'.')}.xml">
<xsl:apply-templates select="json-to-xml($json)"/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XML Output ("so.xml")
<map xmlns="http://www.w3.org/2005/xpath-functions">
<map key="Order">
<map key="InvestmentAccount">
<string key="AccountNumber">10</string>
</map>
<map key="Parcel">
<string key="Limit">0</string>
<string key="ExpiryDate">1900-01-01T00:00:00</string>
<array key="Asset">
<map>
<string key="Open">25.15</string>
<string key="High">25.15</string>
<string key="Low">25.11</string>
<string key="Close">25.87</string>
</map>
<map>
<string key="Open">25.15</string>
<string key="High">25.15</string>
<string key="Low">25.11</string>
<string key="Close">25.87</string>
</map>
</array>
</map>
<string key="OrderDate">2012-10-11T21:46:03.6489906+11:00</string>
</map>
</map>
Since I'm doing an xsl:apply-templates along with the identity transform, the XML from json-to-xml() is output unchanged. I can easily add templates to transform that XML into something else. (Be sure to pay attention to the http://www.w3.org/2005/xpath-functions default namespace.)

Related Links

Exception while loading Java access bridge though java agent
Use com.google.android packages in non-Android Java apps
Why is the main method in superclass executed by JVM? [duplicate]
Why are 3-letter abbreviations for US timezones inconsistent with respect to daylight savings?
How do I create regular expression for a string to match [#username:4]?
Is there any Alternative of --guess in java runcukes
Android Clone Locally Maven Repository
Jackcess how to insert an image with java
Reading a file as a 2d char array
The frequency of items in HashMaps
Activate a plugin from another plugin in Eclipse RCP
How can I set the text inside an EditText to display the first letter of a string? [duplicate]
How can I change the policy of a websocket?
What's the best way to display folder photos with their names and filter in JavaFX? [duplicate]
Using interfaces instead of concrete data structures in Java Collection API
Sqlite error: statements in progress when no statements should be

Categories

HOME
ibm-bluemix
asp.net-core
openstack
gremlin
server
webstorm
rubygems
ip
google-project-tango
echarts
in-app-purchase
uber-api
bs4
vb.net-2010
caml
after-effects
textfield
nas
css-animations
sox
blazemeter
angular2-aot
opennlp
spark-jobserver
status
url-scheme
intel-pin
bootstrap-material-design
filezilla
publish
xacml
large-file-upload
libuv
nat
bosh
.net-4.0
calibre
wixsharp
automake
lto
space-complexity
libraries
serverside-rendering
hp-ux
password-encryption
wso2carbon
hendrix
alphabet
hls.js
svmlight
lowpass-filter
nodebb
boost-preprocessor
capacity
orthogonal
underscore.js-templating
firebaseui
websitepanel
superstack
apachebench
system.management
unity-networking
tcpserver
fouc
ibaction
sysinternals
sankey-diagram
vstest.console.exe
processmodel
sortedlist
system32
notify
gadt
java.util.concurrent
file-copying
rdtsc
has-many-through
article
starcluster
uitouch
doskey
terminal-services
ftps
gridcontrol
yui-compressor
seed
http-unit
newtonscript
ocx
asyncfileupload
servicehost
self-extracting
yui-datatable
revisions
mozilla-prism
xsdobjectgen
preference
photoshop-cs4
anti-piracy
signal-handling
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App