XSLT CODE WORKING : SOIP
<?xml version="1.0" encoding="UTF-8"?>
<M_856>
<S_ST>
<D_143>856</D_143>
<D_329>0001</D_329>
</S_ST>
<S_BSN>
<D_353>00</D_353>
<D_396>0080002121</D_396>
<D_373>20230627</D_373>
<D_337>1037</D_337>
</S_BSN>
<S_DTM>
<D_374>011</D_374>
<D_373>20220610</D_373>
<D_337>000000</D_337>
</S_DTM>
<G_HL>
<S_HL>
<D_628>1</D_628>
<D_735>S</D_735>
<D_736>1</D_736>
</S_HL>
<S_TD1>
<D_80>Constant</D_80>
<D_187>Constant</D_187>
<D_81>Constant</D_81>
</S_TD1>
<S_TD5>
<D_66>ZZ</D_66>
</S_TD5>
<S_REF>
<D_128>Constant</D_128>
<D_127>Constant</D_127>
</S_REF>
<G_N1>
<S_N1>
<D_98>Constant</D_98>
<D_66>Constant</D_66>
<D_67>Constant</D_67>
</S_N1>
</G_N1>
<G_N1>
<S_N1>
<D_98>Constant</D_98>
<D_93>Constant</D_93>
<D_66>Constant</D_66>
<D_67>Constant</D_67>
</S_N1>
</G_N1>
</G_HL>
<G_HL>
<S_HL>
<D_628>2</D_628>
<D_734>1</D_734>
<D_735>O</D_735>
<D_736>1</D_736>
</S_HL>
<S_PRF>
<D_324>test</D_324>
<D_328>Constant</D_328>
<D_327>Constant</D_327>
</S_PRF>
</G_HL>
<G_HL> /
<S_HL>
<D_628>Constant</D_628>
<D_734>Constant</D_734>
<D_735>I</D_735>
</S_HL>
<S_LIN>
<D_350>000010</D_350>
<D_235>PO</D_235>
<D_234>test</D_234>
<D_235_2>VN</D_235_2>
<D_234_2>000000002000000128</D_234_2>
<D_235_3>IN</D_235_3>
<D_234_3>000000002000000128</D_234_3>
</S_LIN>
<S_PO4>
<D_356>10.000</D_356>
<D_357>1</D_357>
<D_355>KGM</D_355>
</S_PO4>
</G_HL> <!--//bring all G_HL[S_HL/D_735='P' and S_LIN/D_350= Preseeding G_HL[S_HL/D-735='I']/S_LIN/D_350 Segments ) for each G_HL[S_HL/D_735=I]-->
<G_HL>
<S_HL>
<D_628>Constant</D_628>
<D_734>Constant</D_734>
<D_735>I</D_735>
</S_HL>
<S_LIN>
<D_350>000020</D_350>
<D_235>PO</D_235>
<D_234>test</D_234>
<D_235_2>VN</D_235_2>
<D_234_2>000000002000000128</D_234_2>
<D_235_3>IN</D_235_3>
<D_234_3>000000002000000128</D_234_3>
</S_LIN>
<S_PO4>
<D_356>10.000</D_356>
<D_357>1</D_357>
<D_355>KGM</D_355>
</S_PO4>
</G_HL>
<G_HL>
<S_HL>
<D_628>Constant</D_628>
<D_734>Constant</D_734>
<D_735>P</D_735>
</S_HL>
<S_LIN>
<D_350>000010</D_350>
<D_235>BN</D_235>
<D_234>00000000000000800266</D_234>
<D_235_2>CH</D_235_2>
<D_235_3>ZZ</D_235_3>
<D_234_3>Y</D_234_3>
</S_LIN>
<S_SN1>
<D_382>10.000</D_382>
<D_355>KGM</D_355>
</S_SN1>
</G_HL>
<G_HL>
<S_HL>
<D_628>Constant</D_628>
<D_734>Constant</D_734>
<D_735>P</D_735>
</S_HL>
<S_LIN>
<D_350>000020</D_350>
<D_235>BN</D_235>
<D_234>00000000000000800267</D_234>
<D_235_2>CH</D_235_2>
<D_235_3>ZZ</D_235_3>
<D_234_3>Y</D_234_3>
</S_LIN>
<S_SN1>
<D_382>10.000</D_382>
<D_355>KGM</D_355>
</S_SN1>
</G_HL>
<S_SE>
<D_96>50</D_96>
<D_329>0001</D_329>
</S_SE>
</M_856>
----------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<!-- Match the root element and apply templates to its child elements -->
<xsl:template match="/">
<M_856>
<xsl:apply-templates select="M_856/*"/>
</M_856>
</xsl:template>
<!-- Match G_HL elements where S_HL/D_735 = 'I' -->
<xsl:template match="S_ST">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="S_BSN">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="S_DTM">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="G_HL[S_HL/D_735='S']">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="G_HL[S_HL/D_735='O']">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="G_HL[S_HL/D_735='I']">
<xsl:copy-of select="."/>
<xsl:for-each select="../G_HL[S_HL/D_735='P' and S_LIN/D_350 = current()/S_LIN/D_350]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()">
</xsl:template>
</xsl:stylesheet>
-------------------
<?xml version="1.0" encoding="UTF-8"?>
<M_856>
<S_ST>
<D_143>856</D_143>
<D_329>0001</D_329>
</S_ST>
<S_BSN>
<D_353>00</D_353>
<D_396>0080002121</D_396>
<D_373>20230627</D_373>
<D_337>1037</D_337>
</S_BSN>
<S_DTM>
<D_374>011</D_374>
<D_373>20220610</D_373>
<D_337>000000</D_337>
</S_DTM>
<G_HL>
<S_HL>
<D_628>1</D_628>
<D_735>S</D_735>
<D_736>1</D_736>
</S_HL>
<S_TD1>
<D_80>Constant</D_80>
<D_187>Constant</D_187>
<D_81>Constant</D_81>
</S_TD1>
<S_TD5>
<D_66>ZZ</D_66>
</S_TD5>
<S_REF>
<D_128>Constant</D_128>
<D_127>Constant</D_127>
</S_REF>
<G_N1>
<S_N1>
<D_98>Constant</D_98>
<D_66>Constant</D_66>
<D_67>Constant</D_67>
</S_N1>
</G_N1>
<G_N1>
<S_N1>
<D_98>Constant</D_98>
<D_93>Constant</D_93>
<D_66>Constant</D_66>
<D_67>Constant</D_67>
</S_N1>
</G_N1>
</G_HL>
<G_HL>
<S_HL>
<D_628>2</D_628>
<D_734>1</D_734>
<D_735>O</D_735>
<D_736>1</D_736>
</S_HL>
<S_PRF>
<D_324>test</D_324>
<D_328>Constant</D_328>
<D_327>Constant</D_327>
</S_PRF>
</G_HL>
<G_HL> /
<S_HL>
<D_628>Constant</D_628>
<D_734>Constant</D_734>
<D_735>I</D_735>
</S_HL>
<S_LIN>
<D_350>000010</D_350>
<D_235>PO</D_235>
<D_234>test</D_234>
<D_235_2>VN</D_235_2>
<D_234_2>000000002000000128</D_234_2>
<D_235_3>IN</D_235_3>
<D_234_3>000000002000000128</D_234_3>
</S_LIN>
<S_PO4>
<D_356>10.000</D_356>
<D_357>1</D_357>
<D_355>KGM</D_355>
</S_PO4>
</G_HL>
<G_HL>
<S_HL>
<D_628>Constant</D_628>
<D_734>Constant</D_734>
<D_735>P</D_735>
</S_HL>
<S_LIN>
<D_350>000010</D_350>
<D_235>BN</D_235>
<D_234>00000000000000800266</D_234>
<D_235_2>CH</D_235_2>
<D_235_3>ZZ</D_235_3>
<D_234_3>Y</D_234_3>
</S_LIN>
<S_SN1>
<D_382>10.000</D_382>
<D_355>KGM</D_355>
</S_SN1>
</G_HL>
<G_HL>
<S_HL>
<D_628>Constant</D_628>
<D_734>Constant</D_734>
<D_735>I</D_735>
</S_HL>
<S_LIN>
<D_350>000020</D_350>
<D_235>PO</D_235>
<D_234>test</D_234>
<D_235_2>VN</D_235_2>
<D_234_2>000000002000000128</D_234_2>
<D_235_3>IN</D_235_3>
<D_234_3>000000002000000128</D_234_3>
</S_LIN>
<S_PO4>
<D_356>10.000</D_356>
<D_357>1</D_357>
<D_355>KGM</D_355>
</S_PO4>
</G_HL>
<G_HL>
<S_HL>
<D_628>Constant</D_628>
<D_734>Constant</D_734>
<D_735>P</D_735>
</S_HL>
<S_LIN>
<D_350>000020</D_350>
<D_235>BN</D_235>
<D_234>00000000000000800267</D_234>
<D_235_2>CH</D_235_2>
<D_235_3>ZZ</D_235_3>
<D_234_3>Y</D_234_3>
</S_LIN>
<S_SN1>
<D_382>10.000</D_382>
<D_355>KGM</D_355>
</S_SN1>
</G_HL>
</M_856>
Tried:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity transform to copy all nodes and attributes -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Match G_HL elements where S_HL/D_735 = 'I' -->
<xsl:template match="G_HL[S_HL/D_735='I' or S_HL/D_735='P']">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:apply-templates select="../G_HL[S_HL/D_735='P' and S_LIN/D_350 = current()/S_LIN/D_350]"/>
</xsl:template>
</xsl:stylesheet>
------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity transform to copy all nodes and attributes -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Match G_HL elements where S_HL/D_735 = 'I' -->
<xsl:template match="G_HL[S_HL/D_735='I']">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::G_HL[S_HL/D_735='P'][1][S_LIN/D_350 = current()/S_LIN/D_350]"/>
</xsl:template>
</xsl:stylesheet>