I am trying to use XSLT to remove ancestor tags (and their children) when they have empty text and a specific attribute value. I have XSLT that checks the text() of each node and when it is empty and the ancestor has attribute deltaxml:deltaV2="A" I want to remove the ancestor and children nodes.
Here is the xml tags I want to remove (note: the ancestor can be anything not just 'p'). In this case I want the last p tag and children removed:
<body>
<p deltaxml:deltaV2="A=B">
<t>This is the same</t>
</p>
<p deltaxml:deltaV2="B">
<t>This is inserted</t>
</p>
<p deltaxml:deltaV2="A">
<t>This is deleted</t>
</p>
<p deltaxml:deltaV2="A">
<t> </t>
</p>
</body>
And here is the XSLT I have so far:
<xsl:template match="@* | * | processing-instruction() | comment()" mode="#all">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@*, node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:variable name="deltaV2" as="attribute()" select="ancestor::*[@deltaxml:deltaV2][1]/@deltaxml:deltaV2"/>
<xsl:variable name="text" select="."/>
<xsl:choose>
<xsl:when test="$deltaV2 eq 'A'">
<xsl:choose>
<xsl:when test="$text = ' '">
<!-- need to remove ancestor tags-->
</xsl:when>
<xsl:otherwise>
<xsl:element name="delete" namespace="{$root-ns}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="$deltaV2 eq 'B'">
<xsl:element name="insert" namespace="{$root-ns}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
Here is desired output:
<body>
<p deltaxml:deltaV2="A=B">
<t>This is the same</t>
</p>
<p deltaxml:deltaV2="B">
<t><insert>This is inserted</insert></t>
</p>
<p deltaxml:deltaV2="A">
<t><delete>This is deleted</delete></t>
</p>
</body>
The reason I need this is because those attributes show whether something was inserted or deleted between 2 versions of XML, but if there was an empty node (ie. the empty t tags in the sample) I don't want to track that as a change since no text has changed, and just want that removed. What do I need to put when the text is empty to be able to remove those tags?