How to remove the child elements,nodes and parent elements in the xml with the help of xsl.
here is my xml code.
<?xml version="1.0" encoding="UTF-8"?>
<Checkpax xmlns="http://xml.api.com/test">
<customerLevel>
<surname>MUKHERJEE</surname>
<type>A</type>
<gender>M</gender>
<otherPaxDetails>
<givenName>JOY</givenName>
<title>MR</title>
<age>11</age>
</otherPaxDetails>
<otherPaxDetails>
<title>MR</title>
</otherPaxDetails>
<staffDetails>
<staffInfo/>
<staffCategoryInfo>
<attributeDetails>
<attributeType>NA</attributeType>
</attributeDetails>
</staffCategoryInfo>
</staffDetails>
<productLevel>
<legLevel>
<legLevelIndicator>
<statusDetails>
<indicator>abc</indicator>
<action>1</action>
</statusDetails>
</legLevelIndicator>
</legLevel>
</productLevel>
<CustomerLevel>
<legLevel>
<legLevelIndicator>
<statusDetails>
<indicator>cde</indicator>
<action>1</action>
</statusDetails>
</legLevelIndicator>
</legLevel>
</CustomerLevel>
</customerLevel>
</Checkpax>
The expected output xml :
<Checkpax xmlns="http://xml.api.com/test">
<paxDetails>
<surname>MUKHERJEE</surname>
<gender>M</gender>
</paxDetails>
<otherPaxDetails>
<title>MR</title>
<age>11</age>
</otherPaxDetails>
<otherPaxDetails>
<title>MR</title>
</otherPaxDetails>
<staffDetails>
<staffCategoryInfo>
<attributeDetails>
<attributeType>NA</attributeType>
</attributeDetails>
</staffCategoryInfo>
</staffDetails>
<legLevelIndicator>
<statusDetails>
<indicator>abc</indicator>
</statusDetails>
</legLevelIndicator>
<CustomerLevel>
<legLevel>
<legLevelIndicator>
<indicator>cde</indicator>
<action>1</action>
</legLevelIndicator>
</legLevel>
</CustomerLevel>
</Checkpax>
The XSL i tried from my end :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://xml.api.com/test"
xmlns:ns0="http://xml.api.com/test"
exclude-result-prefixes="ns0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Apply all child nodes; don't copy the element itself -->
<xsl:template match="ns0:customerLevel| ns0:customerDetails| ns0:paxDetails">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
Kindly suggest the xsl stylesheet for get the expected xml output.i dont know the remianing things how to remove the element.also this will help lot for many people who are looking for the element removal in xml with xsl.
AFAICT, your second template needs to be:
<xsl:template match="ns0:productLevel | ns0:legLevel">
<xsl:apply-templates/>
</xsl:template>
This will remove the productLevel
and legLevel
wrappers, and elevate legLevelIndicator
to be a sibling of staffDetails
.
give me some suggestions how to remove the
<age>11</age>
tag under the otherPaxDetails
Add an empty template matching age
:
<xsl:template match="ns0:age"/>
If you may have other elements with the same name, then narrow down the match pattern:
<xsl:template match="ns0:otherPaxDetails/ns0:age"/>