如果未找到特定元素作为其后代,则更改元素

时间:2014-08-14 14:42:53

标签: xml xslt xslt-2.0

请建议。如果元素'mfenced'没有像'munderover'或'mfrac'这样的元素作为其后代,则需要将其更改为元素'mo'。 在xml中找到'text1'和'text2'文本,在这些区域中,我的代码无法更改为'mo'。

输入XML:

    <article>
    <math>
        <mi>P</mi>
        <mfenced open="(" close=")"><!--To be changed to 'mo', bcs 'mfenced' not having mfrac or munderover as its descendant-->
            <mi>g</mi>
        </mfenced>
        <mfenced open="[" close="]"><!--To be retained as it is, bcs, mfenced having either mfrac or munderover as its descendant-->
            <mfenced open="(" close=")"><!--To be retained as it is, bcs, mfenced having either mfrac or munderover as its descendant-->
                <mi>g</mi>
                <mo>=</mo>
                <munderover>
                    <mrow><mn>1</mn></mrow>
                    <mfenced open="(" close=")"><!--To be changed to 'mo', bcs 'mfenced' not having mfrac or munderover as its descendant-->
                        <mrow><mi>text1</mi></mrow>
                    </mfenced>
                    <mrow><mi>N</mi></mrow>
                </munderover>
            </mfenced>
            <mi>N</mi>
        </mfenced>
        <mfenced open="[" close="]"><!--To be retained as it is, bcs, mfenced having either mfrac or munderover as its descendant-->
            <mfenced open="(" close=")"><!--To be retained as it is, bcs, mfenced having either mfrac or munderover as its descendant-->
                <mfrac>
                    <mrow>
                        <mfenced open="(" close=")"><!--To be changed to 'mo', bcs not having mfrac or munderover as its descendant-->
                            <mi>text2</mi>
                        </mfenced>
                    </mrow>
                    <mrow><mo>2</mo></mrow>
                </mfrac>
            </mfenced>
        </mfenced>
    </math>
</article>

XSLT:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" 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:choose>
        <xsl:when test="descendant::mfrac|descendant::munderover">
                <xsl:copy-of select="."/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:element name="mo">
                <xsl:value-of select="@open"/>
            </xsl:element>
            <xsl:apply-templates/>
            <xsl:element name="mo">
                <xsl:value-of select="@close"/>
            </xsl:element>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template match="//comment"/>
</xsl:stylesheet>

必填结果:

    <article>
   <math>
      <mi>P</mi>
      <mo>(</mo>
        <mi>g</mi>
      <mo>)</mo>
      <mfenced open="[" close="]">
          <mfenced open="(" close=")">
              <mi>g</mi>
            <mo>=</mo>
            <munderover>
               <mrow>
                  <mn>1</mn>
               </mrow>
               <mo>(</mo><!--My code failing here-->
                  <mi>text1</mi>
               <mo>)</mo>
               <mrow>
                  <mi>N</mi>
               </mrow>
            </munderover>
         </mfenced>
         <mi>N</mi>
      </mfenced>
      <mfenced open="[" close="]">
         <mfenced open="(" close=")">
            <mfrac>
               <mrow>
                  <mo>(</mo><!--My code failing here-->
                      <mi>text2</mi>
                  <mo>)</mo>
               </mrow>
               <mrow>
                  <mo>2</mo>
               </mrow>
            </mfrac>
         </mfenced>
      </mfenced>
   </math>
</article>

1 个答案:

答案 0 :(得分:2)

你走在正确的轨道上。我改变了以下内容:

  • copy-of替换为定向apply-templates,后者将为所有其他节点选择身份模板
  • 将最后一次模板匹配更改为comment(),因为您希望捕获注释,而不是称为“注释”的元素节点,我会说。不需要//

你的方法出了什么问题?它没有考虑嵌套 mfenced元素,因为一旦触发了mfenced的模板匹配,您只需复制其所有内容,包括所有潜在的mfenced元素它

<强>样式表

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" 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:choose>
        <xsl:when test="descendant::mfrac|descendant::munderover">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:when>
        <xsl:otherwise>
            <mo>
                <xsl:apply-templates select="@* | node()"/>
            </mo>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template match="comment()"/>
</xsl:stylesheet>

<强>输出

<article>
   <math>
      <mi>P</mi>
      <mo open="(" close=")">
         <mi>g</mi>
      </mo>
      <mfenced open="[" close="]">
         <mfenced open="(" close=")">
            <mi>g</mi>
            <mo>=</mo>
            <munderover>
               <mrow>
                  <mn>1</mn>
               </mrow>
               <mo open="(" close=")">
                  <mrow>
                     <mi>text1</mi>
                  </mrow>
               </mo>
               <mrow>
                  <mi>N</mi>
               </mrow>
            </munderover>
         </mfenced>
         <mi>N</mi>
      </mfenced>
      <mfenced open="[" close="]">
         <mfenced open="(" close=")">
            <mfrac>
               <mrow>
                  <mo open="(" close=")">
                     <mi>text2</mi>
                  </mo>
               </mrow>
               <mrow>
                  <mo>2</mo>
               </mrow>
            </mfrac>
         </mfenced>
      </mfenced>
   </math>
</article>