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>
1

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but it won't always be a 'para0' that i'm matching. It could be any number of elements that the 'appic' tag follows. So i need to remove an unknown element, not specifically 'para0'.
Ok, how about now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.