2017-03-28 6 views
0

MFRACをMSUBの子孫として持つかどうかを確認することをお勧めします。 (一つの属性を挿入)MFENCEDを伸ばすために、以下のように述べ他の要素の最初または二番目の子孫の下に特定の子孫要素を見つける方法

ここではいくつかのシナリオ:MFRACが見出され、これはMSUB下にない場合

  • は、その後ストレッチMFENCEDために必要
    属性。
  • MFRACがMFENCEDの子孫であり、 がMSUBのFIRST-CHILD-DESCENDANTで見つかった場合は、STRETCHが必要です。
  • 入力xmlから3番目のMATHでは、2番目のMFENCEDはSTRETCHを要求しました.MSUBの2番目の子孫のMFRAC子孫ですが、そのMFENCED内では、MFRACは祖先としてMSUBを持っていません。

新規追加更なる説明:

MFENCED IF(から子孫(この)本MFENCED)任意MFRAC見出され、次いでMFENCEDを伸ばすことができる、ここでMFENCE /祖先べきではない、なぜなら、主なMFENCEに関して、FRACはサブの下に2番目の子孫であった。明確でない場合、私はそれ以上の説明をします。

入力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> 

答えて

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> 
1

MFRACが見つかり、MSUBの下にない場合は、STRECHCH がMFENCEDに必要です。

MFRACは、子孫としてMFENCEDおよびMSUBの FIRST-CHILD-子孫の下で、必要なストレッチが見つかった場合

<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> 
。第MATHで

<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から、第2 MFENCEDストレッチ、MSUBの第2子子孫のもその MFRAC子孫を要するが、その 内MFENCED、MFRACその祖先としてMSUBを有していないと。

大変申し訳ございませんが、ここであなたの英語は分かりません。

+0

先生、最後の点は、第二MFRACは、サブの第二子の子孫です。しかし、MFENCED(このMFENCE内のみ)では、MFRACはMSUBとして祖先を持っていないので、そのMFENCE内でチェックするためにgenerate-id()をコーディングしたのはなぜですか。提案してください。 –

+0

すぐにあなたのコードの助けを借りて、助けてくれてありがとう。 –

+0

XSLT 2.0では、 'generate-id($ X)= generate-id($ Y)'を '$ X is $ Y'として書き直すことができます。 –

関連する問題