Reputation: 1278
Suggest for, how to remove 'mi' containg text 'aaaaa',
For quick review I placed comment in input XML as requirement. (XSLT 2)
Input XML:
<root>
<math>
<msub>
<mrow>
<mi mathcolor="red">aaaaa</mi>
<mi>H</mi>
<mi mathcolor="red">aaaaa</mi><!-- MI (with content 'aaaaa') to be removed, bcs MSUB's first child's last content -->
</mrow>
<mrow>
<mi mathcolor="red">aaaaa</mi><!-- MI (with content 'aaaaa') to be removed, bcs MSUB's second child's first content -->
<mn>2</mn>
<mi mathcolor="red">aaaaa</mi>
</mrow>
</msub>
<mi>log</mi>
<msub>
<mrow>
<mfence>
<mrow>
<mi mathcolor="red">aaaaa</mi>
<mi>H</mi>
<mi mathcolor="red">aaaaa</mi><!-- MI (with content 'aaaaa') to be removed, bcs MSUB's first child's last content -->
</mrow>
</mfence>
</mrow>
<mrow>
<mrow>
<mi mathcolor="red">aaaaa</mi><!-- MI (with content 'aaaaa') to be removed, bcs MSUB's second child's first content -->
<mn>2</mn>
<mi mathcolor="red">aaaaa</mi>
</mrow>
</mrow>
</msub>
</math>
</root>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>
<xsl:template match="msub/*[count(preceding-sibling::*)=1]">
<xsl:variable name="var1" select="descendant-or-self::text()[normalize-space(.)!=''][1]"/>
<xsl:for-each select="mi[contains(., 'aaaa')]">
<xsl:choose>
<xsl:when test="contains($var1, 'aaaa')">
<remove/>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="msub/*[count(preceding-sibling::*)=0]">
<xsl:variable name="var1" select="descendant-or-self::text()[normalize-space(.)!=''][position()=last()]"/>
<xsl:for-each select="mi[contains(., 'aaaa')]">
<xsl:choose>
<xsl:when test="contains($var1, 'aaaa')">
<remove/>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Required Result:
<root>
<math>
<msub>
<mrow>
<mi mathcolor="red">aaaaa</mi>
<mi>H</mi>
<remove/>
</mrow>
<mrow>
<remove/>
<mn>2</mn>
<mi mathcolor="red">aaaaa</mi>
</mrow>
</msub>
<mi>log</mi>
<msub>
<mrow>
<mfence>
<mrow>
<mi mathcolor="red">aaaaa</mi>
<mi>H</mi>
<remove/>
</mrow>
</mfence>
</mrow>
<mrow>
<mrow>
<remove/>
<mn>2</mn>
<mi mathcolor="red">aaaaa</mi>
</mrow>
</mrow>
</msub>
</math>
</root>
Upvotes: 1
Views: 583
Reputation: 52858
You could do something like this...
XML Input
<root>
<math>
<msub>
<mrow>
<mi mathcolor="red">aaaaa</mi>
<mi>H</mi>
<mi mathcolor="red">aaaaa</mi><!-- MI (with content 'aaaaa') to be removed, bcs MSUB's first child's last content -->
</mrow>
<mrow>
<mi mathcolor="red">aaaaa</mi><!-- MI (with content 'aaaaa') to be removed, bcs MSUB's second child's first content -->
<mn>2</mn>
<mi mathcolor="red">aaaaa</mi>
</mrow>
</msub>
<mi>log</mi>
<msub>
<mrow>
<mfence>
<mrow>
<mi mathcolor="red">aaaaa</mi>
<mi>H</mi>
<mi mathcolor="red">aaaaa</mi><!-- MI (with content 'aaaaa') to be removed, bcs MSUB's first child's last content -->
</mrow>
</mfence>
</mrow>
<mrow>
<mrow>
<mi mathcolor="red">aaaaa</mi><!-- MI (with content 'aaaaa') to be removed, bcs MSUB's second child's first content -->
<mn>2</mn>
<mi mathcolor="red">aaaaa</mi>
</mrow>
</mrow>
</msub>
</math>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()[not(self::comment())]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="
msub/mrow[position()=last()]//mi[position()=1 and contains(.,'aaaaa')]|
msub/mrow[position()=1]//mi[position()=last() and contains(.,'aaaaa')]"
priority="1">
<remove/>
</xsl:template>
</xsl:stylesheet>
XML Output
<root>
<math>
<msub>
<mrow>
<mi mathcolor="red">aaaaa</mi>
<mi>H</mi>
<remove/>
</mrow>
<mrow>
<remove/>
<mn>2</mn>
<mi mathcolor="red">aaaaa</mi>
</mrow>
</msub>
<mi>log</mi>
<msub>
<mrow>
<mfence>
<mrow>
<mi mathcolor="red">aaaaa</mi>
<mi>H</mi>
<remove/>
</mrow>
</mfence>
</mrow>
<mrow>
<mrow>
<remove/>
<mn>2</mn>
<mi mathcolor="red">aaaaa</mi>
</mrow>
</mrow>
</msub>
</math>
</root
Upvotes: 1