0

Can anyone please provide me XSLT for XML transformation.

Input:-

<RootElement>
    <JSON>[{"51000510000000000008":"Registro guardado exitosamente."}]</JSON>
</RootElement>

Ouptut:-

<Root>  
    <pair>
        <key>51000510000000000008</key>
        <value>Registro guardado exitosamente.</value>
    </pair>
</Root>

2 Answers 2

2

In XSLT 3.0 you can do the following:

<xsl:function name="f:process-kv-pair">
  <xsl:param name="key" as="xs:anyAtomicValue"/>
  <xsl:param name="value" as="item()*"/>
  <pair>
    <key>{$key}</key>
    <value>{$value}</value>
  </pair>
</xsl:function>

<xsl:template match="JSON">
  <xsl:for-each select="parse-json(.)?*">
    <xsl:sequence select="map:for-each-pair(., f:process-kv-pair#2)"/>
  </xsl:for-each>
</xsl:template>

The precise details depend on exactly what you might find in the JSON input if it's different from your example.

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

Comments

1

One solution would be this:

<?xml version = "1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />

  <xsl:template match="/RootElement">
    <Root>
      <pair>
        <xsl:apply-templates select="JSON" />
      </pair>
    </Root>
  </xsl:template>

  <xsl:template match="JSON">
    <xsl:variable name="inner"  select="substring-before( substring-after(.,'[{'), '}]')" />
    <xsl:variable name="sKey"   select="substring-before( $inner,':')" />
    <xsl:variable name="sValue" select="substring-after ( $inner,':')" />
    <key><xsl:value-of select="substring-before(substring-after ($sKey, '&quot;'), '&quot;')" /></key>
    <value><xsl:value-of select="substring-before(substring-after ($sValue, '&quot;'), '&quot;')" /></value>
  </xsl:template>

</xsl:stylesheet>

It encapsulates all <JSON> nodes in <pair> nodes and splits the JSON-string into two parts.

Result is:

<?xml version="1.0"?>
<Root>
    <pair>
        <key>51000510000000000008</key>
        <value>Registro guardado exitosamente.</value>
    </pair>
</Root>

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.