Xslt 基于其祖先在其祖先的第一个子元素中的存在删除元素
请根据元素(msubup)在其(msubup)祖先(msub或msup)的第一个子元素(position=1)中的祖先(msubup)来建议如何删除元素(mstyle) 请参阅我在XML和XSLT中的注释,以便快速参考。(XSLT2) 输入XML:Xslt 基于其祖先在其祖先的第一个子元素中的存在删除元素,xslt,Xslt,请根据元素(msubup)在其(msubup)祖先(msub或msup)的第一个子元素(position=1)中的祖先(msubup)来建议如何删除元素(mstyle) 请参阅我在XML和XSLT中的注释,以便快速参考。(XSLT2) 输入XML: <root> <math> <msubsup> <mrow><mi>H</mi></mrow> <mstyle width="70%">
<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>
H
2.
案文1
K
2.
+
2.
案文2
K
2.
+
2.
案文3
A.
K
2.
+
XSLT:
所需输出:
<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>
H
2.
案文1
K
2.
+
2.
案文2
K
2.
+
2.
案文3
A.
K
2.
+
应删除元素mstyle,但保留内部元素,谢谢您的建议
<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>
<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>