XSLT:建议其他代码,而不满足所有“何时”条件

时间:2014-07-09 13:46:29

标签: xslt

请建议,我正在使用MathML代码,在'subsup'元素中,它应该有三个元素。但是我之前的脚本在'sub'或'subsup'元素之后插入了一些空格标记(内部过程需要)。通过这个现有的'subsup'将有三个以上的孩子。但是我编写的所有条件,但如果没有满足'何时'条件,则无法编码'其他'代码。如果找到空间,那么我的代码将用'mrow'括起'space and following or precedings',确保msubsup只有三个孩子。

XML:

    <article>
 <body>
 <p>Graphene <inline-formula>
 <math>
 <mtext>yyyy</mtext>
    <msubsup>
        <mi>H</mi>
        <mn>1</mn>
        <mn>2</mn>
    </msubsup>
    <msubsup>
        <mi>A</mi>
        <mspace width="0.04"/>
        <mn>1</mn>
        <mspace width="0.04"/>
        <mn>2</mn>
        <mspace width="0.04"/>
      <mn>2</mn>
    </msubsup>
    <msubsup>
       <mi>B</mi>
       <mn>1</mn>
       <mspace width="0.04"/>
       <mn>2</mn>
       <mspace width="0.04"/>
    </msubsup>
    <msubsup>
      <mi>C</mi>
      <mn>1</mn>
      <mn>7</mn>
      <mspace width="0.04"/>
    </msubsup>
    <msubsup>
      <mi>D</mi>
      <mn>1</mn>
      <mn>2</mn>
      <mspace width="0.04"/>
    </msubsup>
    <msubsup>
      <mi>E</mi>
      <mspace width="0.04"/>
      <mn>1</mn>
      <mspace width="0.04"/>
      <mn>2</mn>
    </msubsup>
 </math>
 <inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="fdr495521ieqn220a39.gif"/>
 </inline-formula>
 </p>
 </body>
 </article>

XSLT:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="@* | node()"/>
       </xsl:copy>
    </xsl:template>
    <xsl:template match="msubsup[count(child::*) &gt; 3]">
      <xsl:element name="{name()}">
        <xsl:for-each select="*"><!--Cheking with each child, grouping mspace with preceded or followed element, ensuring three child with subsup element-->
           <xsl:choose>
              <xsl:when test="position()=1 and name()='mspace' and not(following-sibling::*[name()='mspace'])"><!--Child 1 having space, then it has to enclose by 'mrow' with immediate following element-->
                  <xsl:element name="mrow">
                       <xsl:apply-templates select="self::node()"/>
                      <xsl:apply-templates select="following-sibling::*[1]"/>
                  </xsl:element>
                  <xsl:apply-templates select="following-sibling::*[2]"/>
                  <xsl:apply-templates select="following-sibling::*[3]"/>
                  <xsl:apply-templates select="following-sibling::*[4]"/>
                  <xsl:apply-templates select="following-sibling::*[5]"/>
                  <xsl:apply-templates select="following-sibling::*[6]"/>
                  <xsl:apply-templates select="following-sibling::*[7]"/>
              </xsl:when>
              <xsl:when test="position()=2 and name()='mspace' and not(following-sibling::*[name()='mspace'])"><!--Child 2 having space, then it has to enclose by 'mrow' with immediate preceding element-->
                  <xsl:element name="mrow">
                      <xsl:apply-templates select="preceding-sibling::*"/>
                      <xsl:apply-templates select="self::node()"/>
                  </xsl:element>
                  <xsl:apply-templates select="following-sibling::*|node()"/>
              </xsl:when>
              <xsl:when test="position()=2 and name()='mspace' and following-sibling::*[2][name()='mspace']">
                  <xsl:element name="mrow">
                      <xsl:apply-templates select="preceding-sibling::*"/>
                      <xsl:apply-templates select="self::node()"/>
                  </xsl:element>
                  <xsl:element name="mrow">
                      <xsl:apply-templates select="following-sibling::*[1]"/>
                      <xsl:apply-templates select="following-sibling::*[2]"/>
                  </xsl:element>
                  <xsl:element name="mrow">
                      <xsl:apply-templates select="following-sibling::*[3]"/>
                      <xsl:apply-templates select="following-sibling::*[4]"/>
                      <xsl:apply-templates select="following-sibling::*[5]"/>
                      <xsl:apply-templates select="following-sibling::*[6]"/>
                      <xsl:apply-templates select="following-sibling::*[7]"/>
                  </xsl:element>
              </xsl:when>
              <xsl:when test="position()=2 and name()='mspace' and following-sibling::*[3][name()='mspace']">
                  <xsl:element name="mrow">
                      <xsl:apply-templates select="preceding-sibling::*"/>
                      <xsl:apply-templates select="self::node()"/>
                  </xsl:element>
                      <xsl:apply-templates select="following-sibling::*[1]"/>
                  <xsl:element name="mrow">
                      <xsl:apply-templates select="following-sibling::*[2]"/>
                      <xsl:apply-templates select="following-sibling::*[3]"/>
                      <xsl:apply-templates select="following-sibling::*[4]"/>
                      <xsl:apply-templates select="following-sibling::*[5]"/>
                      <xsl:apply-templates select="following-sibling::*[6]"/>
                      <xsl:apply-templates select="following-sibling::*[7]"/>
                  </xsl:element>
              </xsl:when>
              <xsl:when test="position()=3 and name()='mspace' and not(preceding-sibling::*[name()='mspace'])">
                  <xsl:apply-templates select="preceding-sibling::*[2]"/>
                  <xsl:element name="mrow">
                      <xsl:apply-templates select="preceding-sibling::*[1]"/>
                      <xsl:apply-templates select="self::node()"/>
                  </xsl:element>
                  <xsl:element name="mrow">
                      <xsl:apply-templates select="following-sibling::*|node()"/>
                  </xsl:element>
              </xsl:when>
              <xsl:when test="(position()=4 or position() &gt; 4) and name()='mspace' and not(preceding-sibling::*[name()='mspace'])">
                  <xsl:apply-templates select="preceding-sibling::*[3]"/>
                  <xsl:apply-templates select="preceding-sibling::*[2]"/>
                  <xsl:element name="mrow">
                      <xsl:apply-templates select="preceding-sibling::*[1]"/>
                      <xsl:apply-templates select="self::node()"/>
                      <xsl:apply-templates select="following-sibling::*|node()"/>
                  </xsl:element>
              </xsl:when>
              <!--Please suggest, what may be the 'otherwise' code if top all 'when' conditions not met-->
              <!--xsl:otherwise>
                   <xsl:apply-templates select="self::node()"/>
              </xsl:otherwise-->
          </xsl:choose>
      </xsl:for-each>
    </xsl:element>
   </xsl:template>
 </xsl:stylesheet>

必填代码:查看我的xsl,建议使用“其他”代码。我正在使用XSL2版本

必需的OutPut:

enter code here    <?xml version="1.0" encoding="UTF-8"?>
<article>
<body>
<p>Graphene <inline-formula>
<math>
<mtext>yyyy</mtext>
<msubsup>
  <mi>H</mi>
  <mn>1</mn>
  <mn>2</mn>
</msubsup>
<msubsup>
   <mrow>
    <mi>A</mi>
    <mspace width="0.04"/>
   </mrow>
   <mrow>
     <mn>1</mn>
     <mspace width="0.04"/>
   </mrow>
   <mrow>
    <mn>2</mn>
    <mspace width="0.04"/>
    <mspace width="0.04"/>
   <mn>2</mn>
   </mrow>
</msubsup>
<msubsup>
  <mi>B</mi>
  <mrow>
  <mn>1</mn>
  <mspace width="0.04"/>
  </mrow>
  <mrow>
  <mn>2</mn>
  <mspace width="0.04"/>
  </mrow>
</msubsup>
<msubsup>
<mi>C</mi>
<mn>1</mn>
<mrow>
<mn>7</mn>
<mspace width="0.04"/>
</mrow>
</msubsup>
<msubsup>
<mi>D</mi>
<mn>1</mn>
<mrow>
<mn>2</mn>
<mspace width="0.04"/>
</mrow>
</msubsup>
<msubsup>
<mrow>
<mi>E</mi>
<mspace width="0.04"/>
</mrow>
<mrow>
<mn>1</mn>
<mspace width="0.04"/>
</mrow>
<mrow>
<mn>2</mn>
</mrow>
</msubsup>
</math>
<inline-graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="fdr495521ieqn220a39.gif"/>
</inline-formula>
</p>
</body>
</article>

0 个答案:

没有答案