0

I need to be able to remove a specific parent node (and its children) depending on the attributes contained within the child element <version> of the paragraph. So in the below example, i need XSLT to find the instance of <version version="ABCD"> and remove everything from the parent <para0> element. In other words i'm trying to accomplish conditional text filtering. The element i need to match (and remove) will ALWAYS be the parent of <applic> but might not always be a <para0> as in the example, so i need to specify somehow that it needs to match the parent of the 'applic' element rather than explicitly specify the para0.

It should be clearer from the example. I need to remove all the para0 data with a version attribute of ABCD.

So this is some sample XML

<root>
   <para0>
     <applic>
      <model>
      <version version="ABCD"></version>
      </model>
     </applic>
        <subpara1><title>First Title</title>
          <para>Some text relating to ABCD configuration</para>
        </subpara1>
        <subpara1><title>Second Title</title>
          <para>Some other text and stuff relating to ABCD configuration</para>
        </subpara1>
   </para0>
   <para0>
     <applic>
       <model>
       <version version="TRAINING"></version>
       </model>
     </applic>
         <subpara1><title>First Title</title>
           <para>Some text relating to TRAINING configuration</para>
         </subpara1>
         <subpara1><title>Second Title</title>
          <para>Some other text and stuff relating to TRAINING configuration</para>
         </subpara1>
   </para0>
</root>

Here is the XSLT i have so far, but i need it, once matched to ABCD, to basically select and delete the parent of 'applic' and all the child nodes.

       <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method = "xml" indent="yes"/>
        <xsl:strip-space elements = "*" />

        <xsl:template match = "@*|node()" >
          <xsl:copy>
             <xsl:apply-templates select = "@*|node()" />
         </xsl:copy>
        </xsl:template>

        <xsl:template match = "//applic/model[@*]/version[@version='ABCD']" />

        </xsl:stylesheet>
Can'tCodeWon'tCode
  • 543
  • 1
  • 11
  • 36

1 Answers1

0

Your "removing" template must match the element you want to remove - not one of its descendants.

Try:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*[applic/model/version/@version='ABCD']" />

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51