Search code examples
xsltxslt-3.0

XSLT mapping to remove segment based on Key field value


i am trying to write XSLT3.0 mapping to delete Transmission segment when its subsegment Transmission1 has the key value //CAN BE REMOVED//, I am new to XSLT code, tried few but not able to get the desired output, Please assist me on this..

i attached sample input and output as below. Please check.

Input:

<HEADER>
    <Handle>
        
        <Subsegment>           
            <Transmission>
                <Transmission1 LC="PO">123</Transmission1>
            </Transmission>
            <Transmission>
                <Transmission1 LC="PO">456</Transmission1>
            </Transmission>
            <Transmission>
                <Transmission1 LC="PO">789 //CAN BE REMOVED//</Transmission1>
            </Transmission>
             <Transmission>
                <Transmission1 LC="PO">1011</Transmission1>
            </Transmission>
            
        </Subsegment>
    </Handle>
</HEADER>

** Desired Output:**

<HEADER>
    <Handle>
        
        <Subsegment>           
            <Transmission>
                <Transmission1 LC="PO">123</Transmission1>
            </Transmission>
            <Transmission>
                <Transmission1 LC="PO">456</Transmission1>
            </Transmission>
           
        <Transmission>
                <Transmission1 LC="PO">1011</Transmission1>
            </Transmission>
            
        </Subsegment>
    </Handle>
</HEADER>

** XSLT I used is below:**

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="Transmission"> 
      <xsl:apply-templates/>
</xsl:template>

<xsl:template match="TEXT">
        <Transmission1>
                        <xsl:copy-of select="//Transmission1[*//CAN BE REMOVED//]"/>  
            </Transmission1>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>  

Please help me here.


Solution

  • You can use the identity transformation as a starting point and then you need to write an empty template matching that element or those elements you want to removed/prevented from being copied e.g. <xsl:template match="Transmission[Transmission1[contains(., '//CAN BE REMOVED//')]]"/> (that conditions might need to be adjusted, it is not quite clear whether you are looking for a particular value and whether //CAN BE REMOVED// is meant as data in your XML or as a comment to indicate which element you want to be removed but you could of course as well use e.g. <xsl:template match="Transmission[Transmission1[. = 789]]"/> if the number value is what you are looking for.

    So the whole XSLT 3 code amounts to e.g.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="3.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="#all">
    
      <xsl:mode on-no-match="shallow-copy"/>
    
      <xsl:template match="Transmission[Transmission1[contains(., '//CAN BE REMOVED//')]]"/>
    
    </xsl:stylesheet>
    

    or e.g.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="3.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="#all">
    
      <xsl:mode on-no-match="shallow-copy"/>
    
      <xsl:template match="Transmission[Transmission1[. = 789]]"/>
    
    </xsl:stylesheet>
    

    Example online fiddle is here.