How to remove all empty tag with conditons in XSL



I want to remove empty tags with exception.

I made an XSL to remove all empty tags with exception. Following the XSL below:



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="cEAN"/ priority="1">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(@*) and not(*) and (not(text()) or .=-1)]"/>
</xsl:stylesheet>


When the XML following the rules below, it's working fine,



<?xml version="1.0" encoding="utf-8"?>
<NFe xmlns:n0="http://mynamespace">
<cProd>0000000000001</cProd>
<cEAN> </cEAN>
<xProd>ARV NAT</xProd>
<NCM>01000</NCM>
<CFOP>9999</CFOP>
</uCom>
</Nfe>


The transformation XML:



<?xml version="1.0" encoding="UTF-8"?>
<NFe xmlns:n0="http://mynamespace">
<cProd>0000000000001</cProd>
<cEAN />
<xProd>ARV NAT</xProd>
<NCM>01000</NCM>
<CFOP>9999</CFOP>
</NFe>


But when the XML following the rules below, Isn't work.



<?xml version="1.0" encoding="utf-8"?>
<NFe xmlns:n0="http://mynamespace">
<cProd>0000000000001</cProd>
<cEAN/>
<xProd>ARV NAT</xProd>
<NCM>01000</NCM>
<CFOP>999</CFOP>
<uCom/>
</Nfe>


The transformation XML:



<?xml version="1.0" encoding="UTF-8"?>
<NFe xmlns:n0="http://mynamespace">
<cProd>0000000000001</cProd>
<xProd>ARV NAT</xProd>
<NCM>01000</NCM>
<CFOP>9999</CFOP>
</NFe>


The tag cEAN is not preserved and it's remove all empty tags.


Someone could help me?


No comments:

Post a Comment