如何识别在另一个元素的第一或第二后代下找到的特定后代元素

时间:2017-03-28 05:31:52

标签: xslt xslt-2.0

请建议识别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(来自()的后代出现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>

2 个答案:

答案 0 :(得分:1)

  

如果发现MFRAC且不在MSUB下,则属性为STRETCH   MFENCED需要。

<xsl:template match="mfenced[.//mfrac except .//msub//mfrac]">
  <xsl:copy>
   <xsl:attribute name="stretchy">true</xsl:attribute>
   <xsl:apply-templates select="@*, node()"/>
  </xsl:copy>
</xsl:template>
  

如果MFRAC被认为是MFENCED的后代,那么   MSUB的FIRST-CHILD-DESCENDANT,然后需要STRETCH。

<xsl:template match="mfenced[.//msub/*[1]//mfrac]">
  <xsl:copy>
    <xsl:attribute name="stretchy">true</xsl:attribute>
    <xsl:apply-templates select="@*, node()"/>
  </xsl:copy>
</xsl:template>
  

在输入xml的第三个MATH中,第二个MFENCED需要STRETCH,即便如此   MSFR的第2个孩子后裔的MFRAC后代,但在那个内部   MFENCED,MFRAC没有将MSUB作为其祖先。

对不起,我在这里不懂你的英语。

答案 1 :(得分:0)

新XSLT:

<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:choose><!--to check only within MFENCED descendant is MFRAC -->
            <xsl:when test="descendant-or-self::*[matches(name(), '^(msubsup|msub|msup|munder|munderover|mover|msqrt|mroot)$')]/*[not(preceding-sibling::*)]/descendant::mfrac">Yes1</xsl:when>
            <xsl:when test="descendant-or-self::*[matches(name(), '^(msubsup|msub|msup|munder|munderover|mover|msqrt|mroot)$')]/*[(preceding-sibling::*)]/descendant::mfrac">No1</xsl:when>
            <xsl:when test="descendant-or-self::mfrac">Yes2</xsl:when>
        </xsl:choose>
        <!-- to check MFRAC found as online (first child-descendant of msub). -->
        <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>