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

Constructors and how they are utilized
Checkbox setstate using java applet
Getting a java.lang.NoSuchMethodError with netbeans, weblogic, and fast-swap set to true
Spring's ThreadPoolTaskExecutor queue cleared when server goes down?pp
getting a number out of any string
how to store file entries into adjacency list
Reading JSON data using Java
What method to create, in order to delete the rows of information from the database?
Using a different method signature in Java
Convert Infix expression to prefix and postfix expression with Java
Getting exception “ArrayOutOfBounds:1” though it is not accessing any indexout of bounds
How to get a RadioGroup list radiobuttons?
Having troubles creating an iterator for my own list
Searching company name availability using Companies House XML gateway
How to make the camera follow 2 objects?
What is the difference between user create Java API and Java jar?

Categories

HOME
log4j
multithreading
zeromq
urbancode
activiti
push-notification
sqlite-net-extensions
google-oauth
maven-3
survey
fsm
webpack-2
wamp
mouse
podio
ios-charts
directx
bs4
maude-system
adobe-analytics
handsontable
dtrace
facebook-page
code-review
oxyplot
nodatime
conemu
ehcache
telerik-reporting
php-openssl
hammerspoon
shopware
xpages-ssjs
virtualdub
ping
windows-server-2000
primitive
key-value-observing
twilio-api
.net-4.0
karaf
pim
caret
nssegmentedcontrol
ios5
dartium
vapor
webdriver-manager
user-controls
dism
businessworks
qcombobox
wso2carbon
qwt
atomicity
pango
multipeer-connectivity
python-c-api
typescript1.8
integrity
ado.net-entity-data-model
trash
transmitfile
websitepanel
nessus
ipconfig
associative-array
cloudbees
teamcity-8.0
icu4j
sframe
nsight
system32
web-controls
bigbluebutton
google-style-guide
android-imagebutton
phalanger
client-side-templating
internet-connection
rdl
openexr
dealloc
didselectrowatindexpath
centos5
pyhdf
ivyde
seed
android-screen-support
hosts-file
ocx
parametric-equations
removeclass
boost-filesystem
infobox
zend-translate
coda-slider
gin
substrings
webkit.net

Resources

Encrypt Message