1

I met a requirement in which I need to get transfer JSON data into various XML document on the basis of XSLT. In fact, same json data goes to different systems and they have their own object structure (properties nesting level etc) to store it.

I use XslCompiledTransform() in C# to transform Xml into Json; And now looking if there is any efficient way of transforming JSON into XML using XSLT ?

3
  • Your question is too broad I'm afraid. Also, XSLT is perhaps a rather poor choice because it is meant to transform XML documents to something else, not the other way round. If you have a working solution in C# already, why would you need to start using XSLT? Commented Oct 22, 2014 at 8:01
  • @Mathias Müller: Actually, there is a scenario in which same json data goes to different systems and they have their own object structure(properties nesting level etc) to store it. Commented Oct 22, 2014 at 8:06
  • I think we need more details to be able to help you. As XSLT is taking an xml document as input, it shouldn't be your first call to transform JSON to XML. But if you still have to, you could use a dummy XML doc (aka : <dummy/> as content) for input to your XSLT and use the XSLT function : unparsed-text($documentURI,$encodage) to access your JSON. But you'll still need to access the content of your JSON Data as raw string. Commented Oct 22, 2014 at 10:18

3 Answers 3

2

I don't think this will work. JSON is not XML based, so you can't apply XSLT transformations on it. XML to JSON would work, but not JSON to XML

Edit. I was wrong, check this: https://github.com/bramstein/xsltjson and this: How to convert json to xml using xslt

Sign up to request clarification or add additional context in comments.

Comments

0

XSLT is to change one xml document to another xml document, however, json is not even a xml type document..

you can write a simple application to transfer the format

Comments

0

Setting aside the fact that XSLT is definitely not the right tool for that job, here's a pseudo approach to how I'd do it if I ever had to:

  • Create an extension function in C# that does the real job, i.e., getting a JSON string as an argument, returning a generic XPathNodeIterator XML chunk.
  • Process that result normally with XSLT to return the final custom converted format.

The XSLT would then look something like this (assuming XSLT 1.0 since you're in C#):

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:util="urn:JsonUtility.Converter"
>
    <!-- Supplied from environment -->
    <xsl:param name="json" />

    <xsl:template match="/">
        <xsl:variable name="xml" select="util:JSON2XML($json)" />

        <!-- Start processing the returned XML -->
        <xsl:apply-templates select="$xml/json" />
    </xsl:template>

    <xsl:template match="key">
        <!-- output -->
    </xsl:template>

    <xsl:template match="array">
        <!-- output -->
    </xsl:template>

    <!-- etc. -->

</xsl:stylesheet>

(Alternatively, if you create the final format in the C# extension, you could just do a <xsl:copy-of select="$xml" /> in the root template.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.