I need to convert a JSON string to a XML string. The tags do contain attributes. From the answer in this topic I started using XSLT.
There exists a function fn:json-to-xml. I understand that it should convert the JSON into an XML without attributes(which I format using XSLT).
How do I use this function?
Because it's implemented in XSLT I would guess in the .xsl file, but I can't find any examples.
Many thanks in advance!
The function is defined in XSLT 3.0, in XPath 3.1, and in XQuery 3.1.
The simplest way to use it is probably to install Saxon-HE 9.7.0.7 and then run it from XQuery on the command line like this:
Here is a simple example taken from the XSLT 3.0 spec https://www.w3.org/TR/xslt-30/#func-json-to-xml, the sample input can be any string in the JSON format, below I use an XML document containing a
data
element with JSON:The template for the
data
element then simply callsjson-to-xml(.)
and the whole stylesheet to demonstrate the result outputs the returned XML:The output with Saxon 9.7 HE is
So as with any other function taking an input as
as xs:string
you can of course pass in any string value you have in your XSLT or XPath code or you can pass in a node which is then atomized to a string first.