cancel
Showing results for 
Search instead for 
Did you mean: 

How to remove two consecutive root nodes from the payload using XSLT Mapping

vishnu_pallamreddy
Contributor
0 Kudos

Hi All, 

I have  a requirement to remove two consecutive root nodes from the payload. 

Kindly let me know if there is any single XSLT mapping which can used to remove two root nodes at once.

Thanks In Advance,

Vishnu Pallamreddy.

Accepted Solutions (1)

Accepted Solutions (1)

nabendu_sen
Active Contributor
0 Kudos

Check this example, you need to enhance your XSLT as per requirement:

Removing XML Nodes using XSLT - Stack Overflow

Regards,

Nabendu.

vishnu_pallamreddy
Contributor
0 Kudos

Hi Nabendu,

Could please explain about wild card elements in XSLT mapping like *, @*, n*.

For deleting Column and phone node you mentioned code like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

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

<xsl:template match="Column[@SourceColumn='Phone']|Phone" />

</xsl:stylesheet>


But in my case i dont have Column option but I want to delete firstname and lastname nodes then code like this right?

If I am wrong please correct me?


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

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

<xsl:template match="firstname|firstname" />

<xsl:template match="lasstname|lasstname" />

</xsl:stylesheet>

nabendu_sen
Active Contributor
0 Kudos

Hi Vishnu,

Use below code: (Your Lastname spelling was wrong, change if needed)


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

<xsl:output method="xml" indent="yes"/>

<xsl:strip-space elements="*" />

<xsl:template match="@*|node()">

<xsl:copy>

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

</xsl:copy>

</xsl:template>

<xsl:template match="firstname|lasstname" />

</xsl:stylesheet>

Regards,

Nabendu.

vishnu_pallamreddy
Contributor
0 Kudos

Hi Nabendu,

Thank You So much,

If I want to delete 3 consecutive nodes, in our example scenario it is phone, then the code is like below:

Please correct me if I am wrong at the same time could you please help me on wild card elements...

  • <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  • <xsl:output method="xml" indent="yes"/> 
  • <xsl:strip-space elements="*" /> 
  • <xsl:template match="@*|node()"
  • <xsl:copy> 
  •   <xsl:apply-templates select="@*|node()"/> 
  • </xsl:copy> 
  • </xsl:template> 
  • <xsl:template match="firstname|lastname|phone" /> 
  • </xsl:stylesheet>

At the same time please let me know with below code can we delete 2 consecutive root nodes?

for example if I want to delete column and row  root nodes, is it possible. for that below code works?

  • <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  • <xsl:output method="xml" indent="yes"/>
  • <xsl:strip-space elements="*" />
  • <xsl:template match="@*|node()">
  • <xsl:copy>
  •   <xsl:apply-templates select="@*|node()"/>
  • </xsl:copy>
  • </xsl:template>
  • <xsl:template match="column|ROW" />
  • </xsl:stylesheet>
nabendu_sen
Active Contributor
0 Kudos

For 3 consecutive nodes:


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

<xsl:output method="xml" indent="yes"/>

<xsl:strip-space elements="*" />

<xsl:template match="@*|node()">

<xsl:copy>

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

</xsl:copy>

</xsl:template>

<xsl:template match="firstname|lasstname|phone" />

</xsl:stylesheet>

For Wildcard Characters, you can check below blog:

XPath Wildcard

Regards,

Nabendu.

vishnu_pallamreddy
Contributor
0 Kudos

Hi Nabendu,

This code will help me to delete 2 consecutive ROOT Nodes?

nabendu_sen
Active Contributor
0 Kudos

Hi Vishnu,

I thought you asked for 3 Nodes "firstname, lasstname and phone".


If I want to delete 3 consecutive nodes, in our example scenario it is phone, then the code is like below:

Please correct me if I am wrong at the same time could you please help me on wild card elements...

For 2 Nodes, I have already provided the code.

Regards,

Nabendu.

vishnu_pallamreddy
Contributor
0 Kudos

Hi Nabendu,

Thank You.

But my question is If I want to delete 2 ROOT nodes, then same code helps me or not?

Lets take example: MY source

<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
<Rowset>
  
<Details>
  
<FirstName>Michael</FirstName>
  
<LastName>David</LastName>
  
<Phone>1234567890</Phone>
  
</Details>
  
<Details1>
  
<FirstName>David</FirstName>
  
<LastName>Michael</LastName>
  
<Phone>01234567890</Phone>
  
</Details1>
  
<Details2>
  
<FirstName>Yang</FirstName>
  
<LastName>Christina</LastName>
  
<Phone>2345678901</Phone>
  
</Details2>

</Rowset>



I need Target Like below:



<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
<Rowset>
  
<Details>
  
<FirstName>Michael</FirstName>
  
<LastName>David</LastName>
  
<Phone>1234567890</Phone>
  
</Details>

</Rowset>

Former Member
0 Kudos

Are you certain you wish to throw out Details1 and Details2 in your example? Or are you wanting to break the XML into multiple messages?

Either way, you could map to this existing structure and handle the next steps in the mapping. For example, you can map to the structure as is- then in the mapping only take the first instance of Details (ignoring anything which comes after). Or you can handle the mapping so that the receiver gets 3 messages from 1 input XML.

nabendu_sen
Active Contributor
0 Kudos

Hi Vishnu,

Please find the XSLT:

Input----------------


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

<Rowset>

  <Details>

  <FirstName>Michael</FirstName>

  <LastName>David</LastName>

  <Phone>1234567890</Phone>

  </Details>

  <Details1>

  <FirstName>David</FirstName>

  <LastName>Michael</LastName>

  <Phone>01234567890</Phone>

  </Details1>

  <Details2>

  <FirstName>Yang</FirstName>

  <LastName>Christina</LastName>

  <Phone>2345678901</Phone>

  </Details2>

</Rowset>

XSLT Code---------------------


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

<xsl:output method="xml" indent="yes"/>

<xsl:strip-space elements="*" />

<xsl:template match="@*|node()">

<xsl:copy>

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

</xsl:copy>

</xsl:template>

<xsl:template match="Details1|Details2" />

</xsl:stylesheet>

Output-----------------------------


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

<Rowset>

  <Details>

  <FirstName>Michael</FirstName>

  <LastName>David</LastName>

  <Phone>1234567890</Phone>

  </Details>

</Rowset>

Now couple of things, I want to highlight:

1. Your xml did not have Closing brackets for <Rowsets> node, so I removed this node from my example Input xml.

2. If the Node names are dynamic, this XSLT would not work. Because you are hard coding the names in your code:

    <xsl:template match="Details1|Details2" />


In that case, you should go with the idea suggested by


Regards,

Nabendu.

vishnu_pallamreddy
Contributor
0 Kudos

Thank You So Much Nabendu...

Answers (1)

Answers (1)

Former Member
0 Kudos

You should be able to run the same XSLT twice if you define it that way in your operation mapping.

vishnu_pallamreddy
Contributor
0 Kudos

Hi Aaron,

I didn't get your Answer?

Former Member
0 Kudos

If you have the XSLT which strips one root node, assuming the structure is still valid, you can run it through the same XSLT to strip off one deeper level (e.g. new root node). 

Really there is only one root in a document until you've changed it into a new document).

xslt - What is the root node in XML? - Stack Overflow