Need help to write xsl code to exclude a complete xml node based on a certain node.
Requirement is to create the output only if FLDVAL is set to X in child node. Since the FLDVAL is not set to X in the 2nd ZSEGMENT1 node, complete node and its child segments should be excluded. But the first ZSEGMENT and its child nodes should be copied as FLDVAL is set to X in that node.
If FLDVAL is not X in all ZSEGMENT1 node, no output should be created.
Attempt currently:
<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:fo="w3.org/1999/XSL/Format">
<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><!-- Match and output only the ZSEGMENT2 segments meeting the specified condition -->
<xsl:template match="ZSEGMENT2[FLDNAME='CUSTOM' and FLDVAL='X']]">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Input XML
<DELVRY03>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<MANDT>111</MANDT>
<DOCNUM>0000000001234567</DOCNUM>
<DOCREL>731</DOCREL>
<RCVPRN>0000001234</RCVPRN>
</EDI_DC40>
<ZSEGMENT1 SEGMENT="1">
<FLD1>value1</FLD1>
<FLD2>value2</FLD2>
<ZSEGMENT2 SEGMENT="1">
<FLDGRP>ABC</FLDGRP>
<FLDNAME>CUSTOM<< /FLDNAME>
<FLDVAL>X</FLDVAL>
</ZSEGMENT2>
</ZSEGMENT1>
<ZSEGMENT1 SEGMENT="1">
<FLD1>value1</FLD1>
<FLD2>value2</FLD2>
<ZSEGMENT2 SEGMENT="1">
<FLDGRP>ABC</FLDGRP>
<FLDNAME>CUSTOM<< /FLDNAME>
<FLDVAL></FLDVAL>
</ZSEGMENT2>
</ZSEGMENT1>
</IDOC>
</DELVRY03>
Output XML
<DELVRY03>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<MANDT>111</MANDT>
<DOCNUM>0000000001234567</DOCNUM>
<DOCREL>731</DOCREL>
<RCVPRN>0000001234</RCVPRN>
</EDI_DC40>
<ZSEGMENT1 SEGMENT="1">
<FLD1>value1</FLD1>
<FLD2>value2</FLD2>
<ZSEGMENT2 SEGMENT="1">
<FLDGRP>ABC</FLDGRP>
<FLDNAME>CUSTOM<< /FLDNAME>
<FLDVAL>X</FLDVAL>
</ZSEGMENT2>
</ZSEGMENT1>
</IDOC>
</DELVRY03>
user25509459 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.