We want to remove empty Tag that is CommentLine from XML
<CommentLine/>
INPUT XML :
<?xml version="1.0" encoding="WINDOWS-1252"?>
<SalesOrders xsd:noNamespaceSchemaLocation="SDOC.XSD" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">
<Orders>
<OrderHeader>
<CustomerPoNumber>AB-54354</CustomerPoNumber>
</OrderHeader>
<OrderDetails>
<CommentLine>
<Comment>Ensure saddle is color coded</Comment>
<OrderLineID>OR-1810127</OrderLineID>
</CommentLine>
<CommentLine>
<Comment>EDI-001</Comment>
<OrderLineID>OR-1810128</OrderLineID>
</CommentLine>
<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>ABSH-SMH-12OZ-01</StockCode>
<StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
<OrderQty>1.0</OrderQty>
</StockLine>
<CommentLine>
<Comment>This is for test purpose</Comment>
<OrderLineID>OR-1810124</OrderLineID>
</CommentLine>
<CommentLine>
<Comment>EDI-SAVE</Comment>
<OrderLineID>OR-1810125</OrderLineID>
</CommentLine>
<CommentLine/>
</OrderDetails>
</Orders>
</SalesOrders>
Tried XML on it:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="Windows-1252" indent="yes"/>
<xsl:template match="@xsi:nil[.='true']" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<xsl:template match="@*|node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Expected output
<?xml version="1.0" encoding="WINDOWS-1252"?> -<SalesOrders xsd:noNamespaceSchemaLocation="SDOC.XSD" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">
-<Orders>
-<OrderHeader>
<CustomerPoNumber>AB-54354</CustomerPoNumber>
</OrderHeader>
-<OrderDetails>
-<CommentLine> <Comment>Ensure saddle is color coded</Comment>
<OrderLineID>OR-1810127</OrderLineID>
</CommentLine>
-<CommentLine> <Comment>EDI-001</Comment>
<OrderLineID>OR-1810128</OrderLineID>
</CommentLine>
-<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>ABSH-SMH-12OZ-01</StockCode>
<StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
<OrderQty>1.0</OrderQty> </StockLine>
-<CommentLine> <Comment>This is for test purpose</Comment>
<OrderLineID>OR-1810124</OrderLineID>
</CommentLine>
-<CommentLine>
<Comment>EDI-SAVE</Comment>
<OrderLineID>OR-1810125</OrderLineID>
</CommentLine>
</OrderDetails>
</Orders>
</SalesOrders>
we have to remove element in complete Input XML.
Any help would be much appreciated ! Thanks for valuable time.
match="node()[.='']"instead ofmatch="@xsi:nil[.='true']"<OrderDetails><CommentLine/></OrderDetails>. Decide how you want it to behave in that scenario then compare the answers presented with this test data to see what fits (i.e. mine would preserve the empty OrderDetails element; others wouldn't). That could also be addressed by adding other filters (e.g. targeting only elements calledCommentLine).