I have this XML
<math>
<mrow>
<mi>XY</mi>
<mrow>
<mrow>
<mo>(</mo>
<mrow>
<mfrac>
<mn>5</mn>
<mrow>
<mn>10</mn>
</mrow>
</mfrac>
</mrow>
<mo>)</mo>
</mrow>
</mrow>
<msup>
<mn>2</mn>
</msup>
</mrow>
</math>
<br/>
<math>
<mrow>
<mi>x</mi>
<msub>
<mn>1</mn>
</msub>
<msup>
<mn>2</mn>
</msup>
</mrow>
</math>
The following XSL performs a near-identity transformation: it copies everything through, except that it has special handling for msub elements and msup elements. If we have both an msub and an msup in either order, we want to produce a single msupsub element with both of them (and the preceding element, which is the base of the sub- and superscripts). If we have just one, then we produce an msub or msup but bring the immediately preceding sibling in as the first child.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="msup">
<xsl:choose>
<xsl:when test="preceding-sibling::msub">
<msubsup>
<xsl:apply-templates
select="preceding-sibling::*[2]" />
<xsl:apply-templates
select="preceding-sibling::msub[1]/child::*" />
<xsl:apply-templates />
</msubsup>
</xsl:when>
<xsl:when test="following-sibling::msub">
</xsl:when>
<xsl:otherwise>
<msup>
<xsl:apply-templates
select="preceding-sibling::*[1]" />
<xsl:apply-templates />
</msup>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="msub">
<xsl:choose>
<xsl:when test="preceding-sibling::msup">
<msupsub>
<xsl:apply-templates
select="preceding-sibling::*[2]" />
<xsl:apply-templates
select="preceding-sibling::msup[1]/child::*" />
<xsl:apply-templates />
</msupsub>
</xsl:when>
<xsl:when test="following-sibling::msup">
</xsl:when>
<xsl:otherwise>
<msub>
<xsl:apply-templates
select="preceding-sibling::*[1]" />
<xsl:apply-templates />
</msub>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The output I'm currently getting is (roughly):
<xml>
<math display="block">
<mrow>
<mi>lakshmi</mi>
<mrow>
<mrow>
<mo>(</mo>
<mrow>
<mfrac>
<mn>5</mn>
<mrow>
<mn>10</mn>
</mrow>
</mfrac>
</mrow>
<mo>)</mo>
</mrow>
</mrow>
<msup>
<mrow>
<mrow>
<mo>(</mo>
<mrow>
<mfrac>
<mn>5</mn>
<mrow>
<mn>10</mn>
</mrow>
</mfrac>
</mrow>
<mo>)</mo>
</mrow>
</mrow>
<mn>2</mn>
</msup>
</mrow>
</math><br><math display="block">
<mrow>
<mi>x</mi>
<msubsup>
<mi>x</mi>
<mn>1</mn>
<mn>2</mn>
</msubsup>
</mrow>
</math>
</xml>
But in the output above, the element immediately preceding the subscript and/or superscript (the nested mrow in the first math expression, the mi in the second), is being copied through both before and inside the subscript (and/or superscript).
We would like to exclude the preceding-sibling of 'msubsup' and 'msup'. That is, we need output like this, instead of what's shown above:
<xml>
<math display="block">
<mrow>
<mi>lakshmi</mi>
<msup>
<mrow>
<mrow>
<mo>(</mo>
<mrow>
<mfrac>
<mn>5</mn>
<mrow>
<mn>10</mn>
</mrow>
</mfrac>
</mrow>
<mo>)</mo>
</mrow>
</mrow>
<mn>2</mn>
</msup>
</mrow>
</math><br><math display="block">
<mrow>
<msubsup>
<mi>x</mi>
<mn>1</mn>
<mn>2</mn>
</msubsup>
</mrow>
</math>
</xml>
I want to know how to match the preceding-sibling of specific in template. If it is possible to clarify. Thanks for advance.