请建议如何根据其祖先(msubsup)存在于其(msubsup)祖先(msubsup)的第一个子节点(位置= 1)中删除元素(mstyle)。
请参阅我在XML和XSLT中的注释,以便快速参考。 (XSLT2)
输入XML:
<root>
<math>
<msubsup>
<mrow><mi>H</mi></mrow>
<mstyle width="70%"><mrow><mn>2</mn></mrow></mstyle><!-- mstyle to retained because NO ancestor like 'msub' or 'msup' found for msubsup-->
</msubsup>
<mrow><mtext>The text 1</mtext></mrow>
<msub>
<mrow><!--First Child to msub-->
<msubsup>
<mi>K</mi>
<mstyle width="70%"><mn>2</mn></mstyle><!--mstyle required to remove because msubsup found in 'msub's first child area -->
<mo>+</mo>
</msubsup>
</mrow>
<mrow><mn>2</mn></mrow><!--second Child to msub-->
</msub>
<mrow><mtext>The text 2</mtext></mrow>
<msup>
<mrow><!--First Child to msup-->
<mrow>
<msubsup>
<mrow><mi>K</mi></mrow>
<mstyle width="70%"><mrow><mn>2</mn></mrow></mstyle><!--mstyle required to remove because msubsup found in 'msup's first child area -->
<mrow><mo>+</mo></mrow>
</msubsup>
</mrow>
</mrow>
<mrow><mn>2</mn></mrow><!--second Child to msup-->
</msup>
<mrow><mtext>The text 3</mtext></mrow>
<msub>
<mrow><mn>A</mn></mrow><!--First Child to msub-->
<mrow><!--second Child to msub-->
<mrow>
<msubsup>
<mrow><mi>K</mi></mrow>
<mstyle width="70%"><mrow><mn>2</mn></mrow></mstyle><!--mstyle required to RETAIN because msubsup found in 'msub's SECOND child area -->
<mrow><mo>+</mo></mrow>
</msubsup>
</mrow>
</mrow>
</msub>
</math>
</root>
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="mstyle">
<xsl:choose>
<xsl:when test="ancestor::msubsup/ancestor::msub or ancestor::msubsup/ancestor::msup"><!--Checking for msubsup is having ancestor like msub or msup-->
<xsl:choose>
<xsl:when test="ancestor::msubsup/ancestor::*[position()=1]/ancestor::msub or ancestor::msubsup/ancestor::*[position()=1]/ancestor::msup"><!--Remove mstyle element, bcs its ancestor::msubsup found in FIRST child of MSUB or MSUP -->
<xsl:apply-templates/><!--Removing mstyle element only, not its descendants -->
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy><!--Retaining mstyle(ancestor::msubsup), because found at second child of musb or msup -->
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
必需的OutPut:
<root>
<math>
<msubsup>
<mrow><mi>H</mi></mrow>
<mstyle width="70%"><mrow><mn>2</mn></mrow></mstyle><!-- mstyle to retained because NO ancestor like 'msub' or 'msup' found for msubsup-->
</msubsup>
<mrow><mtext>The text 1</mtext></mrow>
<msub>
<mrow>
<msubsup>
<mi>K</mi>
<mn>2</mn>
<mo>+</mo>
</msubsup>
</mrow>
<mrow><mn>2</mn></mrow>
</msub>
<mrow><mtext>The text 2</mtext></mrow>
<msup>
<mrow>
<mrow>
<msubsup>
<mrow><mi>K</mi></mrow>
<mrow><mn>2</mn></mrow>
<mrow><mo>+</mo></mrow>
</msubsup>
</mrow>
</mrow>
<mrow><mn>2</mn></mrow>
</msup>
<mrow><mtext>The text 3</mtext></mrow>
<msub>
<mrow><mn>A</mn></mrow>
<mrow>
<mrow>
<msubsup>
<mrow><mi>K</mi></mrow>
<mstyle width="70%"><mrow><mn>2</mn></mrow></mstyle><!--Required retain mstyle here, bcs msubsup found in second child of msub or msup-->
<mrow><mo>+</mo></mrow>
</msubsup>
</mrow>
</mrow>
</msub>
</math>
</root>
答案 0 :(得分:1)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- The identity transform. -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Match mstyle and output nothing if its msubsup parent is a descendent
of the first child of msub or msup.-->
<xsl:template match="msub/*[1]//msubsup/mstyle|msup/*[1]//msubsup/mstyle">
<!-- Still want to output children of mstyle though. -->
<xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>