Please see the XSLT, input XML, desired output XML and the Actual output, i am getting below.
- I need to REMOVE all the tags if it has no value or whitespace(s).
eg: remove
<abc></abc> - Using the below XSLT, the empty tags are converted to self closing tags (which i totally want to remove), Also the namespaces are getting removed
eg:
<soapenv:Body>gets transformed to<Body>(which i want to keep as it is) - Please help me to achieve the desired output without removing namespace prefix.
XSLT
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="grandParent">
<xsl:copy>
<xsl:apply-templates select="@*" />
<childValues>
<xsl:value-of select="normalize-space(.)" />
</childValues>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Input XML--
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<netconf:rpc xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
message-id="81">
<netconf:edit-config>
<netconf:target>
<netconf:url/>
</netconf:target>
<netconf:config>
<requests module="E5100">
<request action="create" userName="sigma" sessionId="_sessionId">
<SSA>
<NetworkName>NTWK-ntwk_nm</NetworkName>
<PortNumber>2</PortNumber>
<PortType>vdsl</PortType>
<SSAProvision>
<UserDescr></UserDescr>
<SubscriberID></SubscriberID>
</SSAProvision>
</SSA>
</request>
</requests>
</netconf:config>
</netconf:edit-config>
</netconf:rpc>
</soapenv:Body>
</soapenv:Envelope>
Desired Output XML:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<netconf:rpc xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
message-id="81">
<netconf:edit-config>
<netconf:target>
<netconf:url/>
</netconf:target>
<netconf:config>
<requests module="E5100">
<request action="create" sessionId="_sessionId" userName="sigma">
<SSA>
<NetworkName>NTWK-ntwk_nm</NetworkName>
<!-- Port Number removed-->
<PortType>vdsl</PortType>
<SSAProvision>
<UserDescr/>
<SubscriberID/>
</SSAProvision>
</SSA>
</request>
</requests>
</netconf:config>
</netconf:edit-config>
</netconf:rpc>
</soapenv:Body>
</soapenv:Envelope>
Actual Output:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<rpc xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
message-id="81">
<edit-config>
<target>
<url/>
</target>
<config>
<requests module="E5100">
<request action="create" sessionId="_sessionId" userName="sigma">
<SSA>
<NetworkName>NTWK-ntwk_nm</NetworkName>
<!-- want to remove this PortNumber completely-->
<PortNumber/>
<PortType>vdsl</PortType>
<SSAProvision>
<UserDescr/>
<SubscriberID/>
</SSAProvision>
</SSA>
</request>
</requests>
</config>
</edit-config>
</rpc>
</Body>
</Envelope>
grandParentelement in your XML input, so the second template does not do anything.