Home > Software engineering >  Adding parent xml node to json using xslt version 1.0
Adding parent xml node to json using xslt version 1.0

Time:06-17

I need to add parent xml to json content using xslt version 1.0. I have tried multiple ways to get this but no luck.


**Input Content:**
------------------

{
  "EmployerRegistrationReq": {
    "EmployerRegistrationReq": {
      "EmployerRegistrationHeader": {
        "CorporateID": "SIT0001",
        "CorpReferenceNumber": 48499964
                                     }
                                }
                              }
 }


**Expected Output:**
--------------------

<Library>
{
  "EmployerRegistrationReq": {
    "EmployerRegistrationReq": {
      "EmployerRegistrationHeader": {
        "CorporateID": "SIT0001",
        "CorpReferenceNumber": 48499964
                                     }
                                }
                              }
 }
</Library>


Can anyone help me here:

CodePudding user response:

In XSLT 1.0, this will be simple textual manipulation. You won't be able to treat your JSON as a structured item, at least, not without some additional libraries.

If your only problem is to take a "string" value, here a string that can be a serialization of a JSON value, and to make it the text() node child of an element <Library> then the problem is simply one of passing your JSON String text as a parameter to an XSLT 1.0 transformation and then use this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="jsonString" />

<xsl:template match="/" >
    <Library><xsl:value-of select="$jsonString" /></Library>
</xsl:template>

</xsl:stylesheet>

CodePudding user response:

You can use an external entity, I think:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
  <!ENTITY json SYSTEM "json-sample1.json">
]>
<root>&json;</root>

Caveat: use of DTDs/entities might be restricted/disabled these days due to security default settings.

  • Related