【发布时间】:2017-03-28 05:31:51
【问题描述】:
请建议识别 MFENCED,它是否将 MFRAC 作为 MSUB 的后代。
这里提到的一些场景如下拉伸MFENCED(插入一个属性):
- 如果找到 MFRAC 并且不在 MSUB 下,则属性 STRETCH
MFENCED 是必需的。 - 如果发现 MFRAC 是 MFENCED 的后代,并且 在 MSUB 的 FIRST-CHILD-DESCENDANT 下,则需要 STRETCH。
- 在来自输入 xml 的第三个 MATH 中,第二个 MFENCED 需要 STRETCH,即使是 MSUB 的第二个子后代的 MFRAC 后代,但在该 MFENCED 中,MFRAC 没有 MSUB 作为其祖先。
新增进一步说明:
如果MFENCED 在(来自 (this) 的后代存在 MFENCED)下找到任何 MFRAC,则 MFENCED 可以伸展,而 MFENCE/祖先不应伸展,因为相对于主 MFENCE , FRAC 在 sub 的第二个子后裔下找到。如果不清楚,我会给出进一步的解释。
输入 XML:
<article>
<math>
<mrow>
<mfenced open="(" close=")">
<!--stretch required -->
<mrow>
<mrow><mn>99999</mn></mrow>
<mrow>
<mrow><mn>9999</mn></mrow>
<mrow>
<mfenced open="(" close=")">
<!--stretch required -->
<mrow>
<mfrac><mi>a</mi><mi>b</mi></mfrac>
</mrow>
</mfenced>
</mrow>
</mrow>
</mrow>
</mfenced>
</mrow>
</math>
<math>
<mrow>
<mfenced open="(" close=")"><!--Stretch required, bcs descendant Frac, found as first child of msub, if descendant mfrac, found under (descendant) second child of MSUB, then no need to stretch-->
<mrow>
<mrow><mn>99999</mn></mrow>
<mrow>
<msub>
<mrow>
<mfenced open="(" close=")"><!--Stretch required, because under this mfen, 'mfrac' found under first child of 'msub' -->
<mrow>
<mfrac><mi>a</mi><mi>b</mi></mfrac>
</mrow>
</mfenced>
</mrow>
<mrow><mn>9999</mn></mrow>
</msub>
</mrow>
</mrow>
</mfenced>
</mrow>
</math>
<math>
<mrow>
<mfenced open="(" close=")"><!-- this mfence, no need to stretch, because, descendant MFRAC found under second child-descendant of MSUB -->
<mrow>
<mrow><mn>99999</mn></mrow>
<mrow>
<msub>
<mrow><mn>9999</mn></mrow>
<mrow>
<mfenced open="(" close=")"><!--Stretch required, because under this mfen, 'mfrac' found (even MFRAC under 2nd child-descendant of MSUB, but under existing MFENCE, MFRAC is not having ancestor MSUB -->
<mrow>
<mfrac><mi>a</mi><mi>b</mi></mfrac>
</mrow>
</mfenced>
</mrow>
</msub>
</mrow>
</mrow>
</mfenced>
</mrow>
</math>
</article>
XSLT 2.0:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="mfenced">
<xsl:variable name="varFrac">
<xsl:for-each select="descendant::mfenced">
<xsl:for-each select="descendant::*[name()='mfrac']">
<xsl:choose>
<xsl:when test="not(ancestor::*[matches(name(), '^(msubsup|msub|msup|munder|munderover|mover|msqrt|mroot)$')]
[generate-id(ancestor::mmlmfenced[1])=generate-id(current()/ancestor::mmlmfenced[1])])">Yes2</xsl:when>
<xsl:when test="(ancestor-or-self::*/parent::*[not(preceding-sibling::*)]/parent::*[matches(name(), '^(msubsup|msub|msup|munder|munderover|mover|msqrt|mroot)$')]
[generate-id(ancestor::mmlmfenced[1])=generate-id(current()/ancestor::mmlmfenced[1])])">Yes21</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
<xsl:for-each select="descendant::*[name()='mfrac']">
<xsl:choose>
<xsl:when test="not(ancestor::*[matches(name(), '^(msubsup|msub|msup|munder|munderover|mover|msqrt|mroot)$')])">Yes1a</xsl:when>
<xsl:when test="(ancestor-or-self::*/parent::*[not(preceding-sibling::*)]/parent::*[matches(name(), '^(msubsup|msub|msup|munder|munderover|mover|msqrt|mroot)$')])">Yes11a</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($varFrac, 'Yes')">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="stretchy">true</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
要求的结果:
<article>
<math>
<mrow>
<mfenced open="(" close=")" stretchy="true">
<mrow>
<mrow>
<mn>99999</mn>
</mrow>
<mrow>
<mrow>
<mn>9999</mn>
</mrow>
<mrow>
<mfenced open="(" close=")" stretchy="true">
<mrow>
<mfrac>
<mi>a</mi>
<mi>b</mi>
</mfrac>
</mrow>
</mfenced>
</mrow>
</mrow>
</mrow>
</mfenced>
</mrow>
</math>
<math>
<mrow>
<mfenced open="(" close=")" stretchy="true">
<mrow>
<mrow>
<mn>99999</mn>
</mrow>
<mrow>
<msub>
<mrow>
<mfenced open="(" close=")" stretchy="true">
<mrow>
<mfrac>
<mi>a</mi>
<mi>b</mi>
</mfrac>
</mrow>
</mfenced>
</mrow>
<mrow>
<mn>9999</mn>
</mrow>
</msub>
</mrow>
</mrow>
</mfenced>
</mrow>
</math>
<math>
<mrow>
<mfenced open="(" close=")">
<mrow>
<mrow>
<mn>99999</mn>
</mrow>
<mrow>
<msub>
<mrow>
<mn>9999</mn>
</mrow>
<mrow>
<mfenced open="(" close=")" stretchy="true"><!-- Here stretch required -->
<mrow>
<mfrac>
<mi>a</mi>
<mi>b</mi>
</mfrac>
</mrow>
</mfenced>
</mrow>
</msub>
</mrow>
</mrow>
</mfenced>
</mrow>
</math>
</article>
【问题讨论】: