Suppose I have a source example document like this:
<html>
<b><i><u>TestBIU</u></i></b>
<i><b><u>TestIBU</u></b></i>
<i><u><b>TestIUB</b></u></i>
<b><u><i>TestBUI</i></u></b>
<u><i><b>TestUIB</b></i></u>
<u><b><i>TestUBI</i></b></u>
<u>TestU</u>
<i>TestI</i>
<b>TestB</b>
<u><b>TestUB</b></u>
</html>
I need a XSLT-Template that produces this:
<html>
<b><i>TestBIU</i></b>
<i><b>TestIBU</b></i>
<i><b>TestIUB</b></i>
<b><i>TestBUI</i></b>
<i><b>TestUIB</b></i>
<b><i>TestUBI</i></b>
<u>TestU</u>
<i>TestI</i>
<b>TestB</b>
<b>TestUB</b>
</html>
So, it should remove the underline tag when it occurs in combination with italic and/or bold tags. When only underlined, it should remain.
Any ideas how to solve this particular problem?
Here is my attempt, but if fails for TestUIB and TestUBI:
<xsl:template match="/">
<html>
<xsl:apply-templates />
</html>
</xsl:template>
<xsl:template match="b/u">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="i/u">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="u/i">
<i><xsl:apply-templates /></i>
</xsl:template>
<xsl:template match="u/b">
<b><xsl:apply-templates /></b>
</xsl:template>
<xsl:template match="b | u | i">
<xsl:copy>
<xsl:apply-templates select="* | text()"/>
</xsl:copy>
</xsl:template>
Try this:
It produces the following output to your sample:
Description: In my solution I’m using identity transform that copies everything node by node and attribute by attribute. The second template intercepts all
<u>HTML tags that have<i>or<b>amongst their ancestors or descendants. When such situation occurs, we do not copy the tag, but only invoke apply-templates which will take care of its children.