cancel
Showing results for 
Search instead for 
Did you mean: 

XSLT mapping to remove handle nodes

former_member308595
Participant
0 Kudos

Hi Experts ,

I have one requirement. I need to remove Node element from JSON structure with help of XSLT mapping .

Example :

Input JSON input :

{

"ns0:MT_EMP_INFO" : {

"Id" : "",
"FIrstname" : "Lassy",
"LastName" : "Emanu",
"Location" : "UK",
"Entity" : "IT"

}}

Required JSON output :

{
"Id" : "",
"FIrstname" : "Lassy",
"LastName" : "Emanu",
"Location" : "UK",
"Entity" : "IT"

}

Can anyone help to fix this issue . this XSLT code will generic and can remove only

ns0:MT_EMP_INFO from JSON input .


Thanks in advance.



Accepted Solutions (0)

Answers (1)

Answers (1)

Muniyappan
Active Contributor
0 Kudos

same issue has been discussed in this thread.you may use groovy for this. try to adjust the groovy script code to fit for your requiremnt.

https://answers.sap.com/questions/364055/how-to-remove-root-element-in-json-after-conversio.html

former_member308595
Participant
0 Kudos

Hi Muni ,
Thanks for your response, do you know how we can fix trough XSLT code ?

Muniyappan
Active Contributor
0 Kudos

any reason why do you want xslt? please share your xml.

may be you can insert the xslt mapping before xml to json converter.

former_member308595
Participant
0 Kudos

Hi Muni ,
XML input :

XML input :
?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_EMP_INFO xmlns:ns0="http://IBM/Hybris/EMP">
  <EmPInfo>
  <Id> 76433<dID>
  <FirtName>Lussy</last_name>
  <LastName>Emanu</email>
  <Location>UK</country>
  <Entity>IT<Entity>
  </EmPInfo>
</ns0:MT_EMP_INFO



Note : Elements will increase I coated few fileds only

XSLT code :

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

    <xsl:template match="/">{
        <xsl:apply-templates select="*"/>}
    </xsl:template>
    
    <!-- Object or Element Property-->
    <xsl:template match="*">
        "<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/>
    </xsl:template>

    <!-- Array Element -->
    <xsl:template match="*" mode="ArrayElement">
        <xsl:call-template name="Properties"/>
    </xsl:template>

    <!-- Object Properties -->
    <xsl:template name="Properties">
        <xsl:variable name="childName" select="name(*[1])"/>
        <xsl:choose>
            <xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
            <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
            <xsl:otherwise>{
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates select="*"/>
    }</xsl:otherwise>
        </xsl:choose>
        <xsl:if test="following-sibling::*">,</xsl:if>
    </xsl:template>

    <!-- Attribute Property -->
    <xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
    </xsl:template>
</xsl:stylesheet>

Output looks :

{
 
"ns0:MT_EMP_INFO" : {
 
"Id" : "",
"FIrstname" : "Lassy",
"LastName" : "Emanu",
"Location" : "UK",
"Entity" : "IT" 
 
}}

Here I need to remove "ns0:MT_EMP_INFO" field from out put . Here I used XSLT to convert xml into JSON , so I would preffer to use XSLT.



Muniyappan
Active Contributor

looking at the xslt, it seems that it is used as generic. you dont have to use xslt to remove the root node just because xml is converted into json using xslt. Adding groovy script and handling this requirement makes sense to me instead of using xslt.

if you still want to achieve using xslt, either

1.change existing xslt or

2. add one more xslt, read the converted json and then filter the required information which is equivalent to doing sub string like handled in groovy scripting.

if i run your code with given input, i get output like below. your given xml is not valid, has errors.

{
        
        "ns0:MT_EMP_INFO" : {
                
        "EmPInfo" : {
                
        "Id" : "76433",
        "FirtName" : "Lussy",
        "LastName" : "Emanu",
        "Location" : "UK",
        "Entity" : "IT"
    }
    }}
    

Seems you have to modify your existing xsl to get desired.

xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://IBM/Hybris/EMP">
    <xsl:output method="text"/>


    <xsl:template match="/ns0:MT_EMP_INFO">{
        <xsl:apply-templates select="*"/> }
    </xsl:template>
    
    <!-- Object or Element Property-->
    <xsl:template match="*">
	
        "<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/>
		
	
    </xsl:template>


    <!-- Array Element -->
    <xsl:template match="*" mode="ArrayElement">
        <xsl:call-template name="Properties"/>
    </xsl:template>


    <!-- Object Properties -->
    <xsl:template name="Properties">
        <xsl:variable name="childName" select="name(*[1])"/>
        <xsl:choose>
            <xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
            <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
            <xsl:otherwise>{
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates select="*"/>
    }</xsl:otherwise>
        </xsl:choose>
        <xsl:if test="following-sibling::*">,</xsl:if>
    </xsl:template>


    <!-- Attribute Property -->
    <xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
    </xsl:template>
</xsl:stylesheet>



xml

<?xml version="1.0" encoding="UTF-8"?>


<ns0:MT_EMP_INFO xmlns:ns0="http://IBM/Hybris/EMP">


  <Id>76433</Id>
  <FirtName>Lussy</FirtName>
  <LastName>Emanu</LastName>
  <Location>UK</Location>
  <Entity>IT</Entity>


</ns0:MT_EMP_INFO>

output

{
        
	
        "Id" : "76433",
	
        "FirtName" : "Lussy",
	
        "LastName" : "Emanu",
	
        "Location" : "UK",
	
        "Entity" : "IT" }
    

former_member308595
Participant
0 Kudos

HI Muni,

Thank you very much ,here I have some issues while handling the array structure above code.

I got on XSLT to handle array structure , but here same issues it contain output with tag with "/nso:MT_EMP_INFO" but I used your same templet but it runs on errors ,can you correct the below one , we are almost near to solution .

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

  <xsl:template match="/ns0:MT_EMP_INFO">{
        <xsl:apply-templates select="*"/> }
    </xsl:template>
 
 
    <xsl:template match="/*[node()]">
        <xsl:text>{</xsl:text>
        <xsl:apply-templates select="." mode="detect" />
        <xsl:text>}</xsl:text>
    </xsl:template>
 
    <xsl:template match="*" mode="detect">
        <xsl:choose>
            <xsl:when test="name(preceding-sibling::*[1]) = name(current()) and name(following-sibling::*[1]) != name(current())">
                    <xsl:apply-templates select="." mode="obj-content" />
                <xsl:text>]</xsl:text>
                <xsl:if test="count(following-sibling::*[name() != name(current())]) > 0">, </xsl:if>
            </xsl:when>
            <xsl:when test="name(preceding-sibling::*[1]) = name(current())">
                    <xsl:apply-templates select="." mode="obj-content" />
                    <xsl:if test="name(following-sibling::*) = name(current())">, </xsl:if>
            </xsl:when>
            <xsl:when test="following-sibling::*[1][name() = name(current())]">
                <xsl:text>"</xsl:text><xsl:value-of select="name()"/><xsl:text>" : [</xsl:text>
                    <xsl:apply-templates select="." mode="obj-content" /><xsl:text>, </xsl:text>
            </xsl:when>
            <xsl:when test="count(./child::*) > 0 or count(@*) > 0">
                <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : <xsl:apply-templates select="." mode="obj-content" />
                <xsl:if test="count(following-sibling::*) > 0">, </xsl:if>
            </xsl:when>
            <xsl:when test="count(./child::*) = 0">
                <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:apply-templates select="."/><xsl:text>"</xsl:text>
                <xsl:if test="count(following-sibling::*) > 0">, </xsl:if>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
 
    <xsl:template match="*" mode="obj-content">
        <xsl:text>{</xsl:text>
            <xsl:apply-templates select="@*" mode="attr" />
            <xsl:if test="count(@*) > 0 and (count(child::*) > 0 or text())">, </xsl:if>
            <xsl:apply-templates select="./*" mode="detect" />
            <xsl:if test="count(child::*) = 0 and text() and not(@*)">
                <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:value-of select="text()"/><xsl:text>"</xsl:text>
            </xsl:if>
            <xsl:if test="count(child::*) = 0 and text() and @*">
                <xsl:text>"text" : "</xsl:text><xsl:value-of select="text()"/><xsl:text>"</xsl:text>
            </xsl:if>
        <xsl:text>}</xsl:text>
        <xsl:if test="position() < last()">, </xsl:if>
    </xsl:template>
 
    <xsl:template match="@*" mode="attr">
        <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:value-of select="."/><xsl:text>"</xsl:text>
        <xsl:if test="position() < last()">,</xsl:if>
    </xsl:template>
 
    <xsl:template match="node/@TEXT | text()" name="removeBreaks">
        <xsl:param name="pText" select="normalize-space(.)"/>
        <xsl:choose>
            <xsl:when test="not(contains($pText, '
'))"><xsl:copy-of select="$pText"/></xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat(substring-before($pText, '
'), ' ')"/>
                <xsl:call-template name="removeBreaks">
                    <xsl:with-param name="pText" select="substring-after($pText, '
')"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
 
</xsl:stylesheet><br>
former_member308595
Participant
0 Kudos

Hi Muni ,

Any suggestion from your side ?

Best Shiva