請建議識別MFENCAC,MFENCAC是否具有作爲MSUB後代的MFRAC。如何識別在另一個元素的第一或第二後代中找到的特定後代元素
在這裏如後所述以拉伸MFENCED(插入一個屬性)的一些情況:
- 如果MFRAC發現,這是不MSUB下,然後在屬性STRETCH
爲MFENCED必需的。 - 如果MFRAC在MSUB的FIRST-CHILD-DESCENDANT下找到MFENCED和 的後代,則需要STRETCH。
- 在輸入xml中的第三個MATH中,第二個MFENCED需要STRETCH,即使是MSUB的第二個子代後代的MFRAC後代,但在該MFENCED內,MFRAC沒有將MSUB作爲其祖先。
新添加的進一步的解釋:
如果下MFENCED
(後代從(此)本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>
先生,最後一點,第二MFRAC是潛艇的第二個孩子的後代。但在MFENCED內(僅在此MFENCE內),MFRAC沒有祖先作爲MSUB,這就是爲什麼我編寫了一些generate-id()來檢查該MFENCE。請建議。 –
不久,我將得到您的代碼的幫助先生,並感謝您的幫助。 –
請注意,在XSLT 2.0中,您可以將'generate-id($ X)= generate-id($ Y)'重寫爲'$ X是$ Y',這可以簡化很多事情。 –