Transform
priint cloud renderings service allows to transform incoming XML or JSON inputs into target XMLs following the xml-data-format.
Transformations are requested via the transform parameter of the rest-api. See REST API.
Currently, we only support XSLT transformations.
Given a transform parameter value of stylesheet.xslt the transform worker will search for a valid XSLT document in project path /{project}/transform/xslt/mystylesheet.xslt.
JSON Input
It is allowed to use JSON as input.
JSON will be transformed into an XML structure that is then piped into the configured XSLT processor.
There is no existing standard how JSON should be converted into XML - the process has several degrees of freedom. At least the following questions mus be answered:
- JSON allows arrays as direct children of arrays - this only works in XML if the arrays are wrapped with a named element.
- JSON keys are arbitrary unicode strings - in XML an element name must follow the rules of a QName (cannot start with a digit, cannot contain whitespace, etc.). We can handle this by putting all keys into XML attributes thereby making the XML even more verbose
We allow to configure the XML output by the following settings in the config.ion under key jsonToXml:
| Name | Description |
|---|---|
| rootTag | Name of the document element in the XML. |
| discardNulls | Make the processor skip JSON properties with null values. |
| convertKeysToAttributes | JSON key names are added as XML attributes with name 'keyAttributeName' into elements with name 'elementTag' |
| keyAttributeName | See 'convertKeysToAttributes'. |
| elementTag | See 'convertKeysToAttributes'. |
| useArrayWrapper | Each JSON array is wrapped into an XML element named 'arrayWrapperTag' |
| arrayWrapperTag | See 'arrayWrapperTag'. |
Example for a config.ion. It contains the default settings, so using this is the same as ommitting the whole config.ion.
{
"jsonToXml" : {
"rootTag" : "root",
"elementTag" : "item",
"discardNulls" : true,
"convertKeysToAttributes" : false,
"keyAttributeName" : "name",
"useArrayWrapper" : false,
"arrayWrapperTag" : "instance"
}
}
JSON example
{
"id": 57478,
"active": true,
"product name": "08/15",
"props": {
"color": "gray"
},
"nil": null,
"list": [1, 2, 3, ["äöü", {
"3": -1
}
]]
}
Commented XML output for the above JSON example when config.ion is empty:
<root>
<id>57478</id>
<active>true</active>
<product-name>08/15</product-name> <!-- all chars in JSON key not matching regex [\w.-] will be replaced with dash -->
<props>
<color>gray</color>
</props>
<!-- null values in JSON will not appear in XML -->
<list>1</list>
<list>2</list>
<list>3</list>
<list>äöü</list><!-- arrays in arrays are not supported by default -> useArrayWrapper -->
<list>
<_>-1</_><!-- first char in a JSON key must be alphabetic or underscore - we replace with underscore -->
</list>
</root>
Commented XML output for the above JSON example when convertKeysToAttributes is activated:
<item name="root">
<item name="id">57478</item>
<item name="active">true</item>
<item name="product name">08/15</item>
<item name="props">
<item name="color">gray</item>
</item>
<item name="list">1</item>
<item name="list">2</item>
<item name="list">3</item>
<item name="list">äöü</item><!-- arrays in arrays are not supported by default -> useArrayWrapper -->
<item name="list">
<item name="3">-1</item>
</item>
</item>
Commented XML output for the above JSON example when arrayWrapper is activated - here with custom element name:
<root>
<id>57478</id>
<active>true</active>
<product-name>08/15</product-name>
<props>
<color>gray</color>
</props>
<list>
<arr>1</arr>
<arr>2</arr>
<arr>3</arr>
<arr><!-- arrays in arrays are supported here -->
<arr>äöü</arr>
<arr>
<_>-1</_>
</arr>
</arr>
</list>
</root>
Showing the effect on deactivating the discardNulls on the JSON example:
<root>
<id>57478</id>
<active>true</active>
<product-name>08/15</product-name>
<props>
<color>gray</color>
</props>
<nil/> <!-- nil element appears with empty content -->
<list>1</list>
<list>2</list>
<list>3</list>
<list>äöü</list>
<list>
<_>-1</_>
</list>
</root>
- If you know that your JSON has no arrays within arrays and uses simple alphanumeric keys (allowing for underscore or dash as delimiter char) then the default settings are a good choice.
- When you want to be sure that nearly any JSON can be transformed into XML without information loss, then you should use: discardNulls=false, convertKeysToAttributes=true, useArrayWrapper=true. The XML will be more verbose and hence the XPath expressions in an XSL transformation will be more complicated.