Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Add a prefix namespace in XML using XSLT

Not applicable

My Input XML payload :

<d:table xmlns:d="http://abc/xyz/d" xmlns:m="http://abc/xyz/m" m:type="abc.xyz.123">
  <d:name m:type="Edm.String">African Coffee Table</d:name>
  <d:width m:type="Edm.Boolean">true</d:width>
  <d:height m:type="Edm.Double" >120.50</d:height>
  <d:area m:type="Edm.Double" >120.50</d:area>
  <d:volume m:type="Edm.Double" >120.50</d:volume>
  <d:cube m:type="Edm.Double" >120.50</d:cube>
  <d:length m:type="Edm.Double" >120.50</d:length>
</d:table>

Output expected in JSON :

{
  "table": {
    "abc.xyz.123"
    "name": {
      "type": "Edm.String",
      "content": "African Coffee Table"
    }
    "width": {
      "type": "Edm.Boolean",
      "content": "true"
    }
    "length": {
      "type": "Edm.Double",
      "content": "120.50"
    }
  }
}


MY XSLT code :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:d="http://abc/xyz/d" xmlns:m="http://abc/xyz/m">
  <xsl:output method="text"/>
  <xsl:variable name="newline">
    <xsl:text>
    </xsl:text>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:text>
      <table>
      </xsl:text>
      <xsl:value-of select="$newline"/>
      <xsl:for-each select="/d:table">
        <xsl:value-of select="$newline"/>
      </xsl:for-each>
      <xsl:for-each select="/d:table">
        <xsl:text>
          <table>
          </xsl:text>
          <xsl:value-of select="d:table"/>
          <xsl:text>
          </table>
        </xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:text>
          <width>>
        </xsl:text>
        <xsl:value-of select="d:width"/>
        <xsl:text>
        </width>
      </xsl:text>
      <xsl:text>
        <length>>
      </xsl:text>
      <xsl:value-of select="d:length"/>
      <xsl:text>
      </length>
    </xsl:text>
  </xsl:for-each>
  <xsl:text>
  </table>
</xsl:text>
</xsl:template>
</xsl:stylesheet>

I need to map xml payload such that the actual value of tag is added inside content tag and data type is mapped under type.
I will use XML to JSON to convert the transformed XML in to JSON

but as of now i am not able to convert the XML payload into the expected XML out

Please help

Thanks in Advance

Solved Solved
0 4 5,631
1 ACCEPTED SOLUTION

To do this, you can add an XSLT policy before the XMLtoJSON policy to modify the input XML so as to whitelist only the elements you need from the request payload. One way to do this would be in XSLT:

Policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XSL async="false" continueOnError="false" enabled="true" name="XSL-Transform-1">
    <DisplayName>XSL Transform-1</DisplayName>
    <Properties/>
    <Source>request</Source>
    <ResourceURL>xsl://XSL-Transform-1.xsl</ResourceURL>
    <Parameters ignoreUnresolvedVariables="true"/>
    <OutputVariable/>
</XSL>

XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:d="http://abc/xyz/d">
    <xsl:output indent="yes" />
    <d:WhiteList>
        <name>d:table</name>
        <name>d:name</name>
        <name>d:length</name>
        <name>d:width</name>
    </d:WhiteList>

    <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
    </xsl:template>

    <xsl:template match="*[not(descendant-or-self::*[name()=document('')/*/d:WhiteList/*])]"/>

</xsl:stylesheet>

View solution in original post

4 REPLIES 4