子XML元素未使用XSLT移动

子XML元素未使用XSLT移动,xslt,Xslt,我需要移动,元素,如输出所示。我可以移动元素。子元素没有移动。我还需要将元素重命名为和。如果我运行两次XSLT,就可以得到输出。但是我不想那样做。请纠正我的错误。我应该使用XSLT1.0 输入XML: <?xml version='1.0' encoding='UTF-8' ?> <chapter xmlns="http://www.w3.org/1998/Math/MathML"> <math display="block"> <

我需要移动
元素,如输出所示。我可以移动
元素。子元素没有移动。我还需要将元素重命名为
。如果我运行两次XSLT,就可以得到输出。但是我不想那样做。请纠正我的错误。我应该使用XSLT1.0

输入XML:

<?xml version='1.0' encoding='UTF-8' ?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
    <math display="block">
        <mfenced>
            <mrow>
                <mfrac>
                    <mrow>
                        <mi>v</mi>
                        <sub>
                            <mrow><mi>n</mi></mrow>
                        </sub>
                    </mrow>
                    <mrow>
                        <mi>v</mi>
                        <sub>
                            <mrow><mi>d</mi></mrow>
                        </sub>
                    </mrow>
                </mfrac>
            </mrow>
        </mfenced>
        <sup><mrow><mn>2</mn></mrow></sup>
    </math>
</chapter>

v
N
v
D
2.
已尝试的XSLT示例:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns="http://www.w3.org/1998/Math/MathML" exclude-result-prefixes="m">

<xsl:output method="xml"/>
<xsl:template match="@* | node()">
<xsl:choose>
<xsl:when test="self::*/following-sibling::*[1]/self::m:sub"></xsl:when>
<xsl:when test="self::*/following-sibling::*[1]/self::m:sup"></xsl:when>
<xsl:when test="self::*/following-sibling::*[1]/self::m:subsup"></xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="m:sub">
<msub>
<xsl:copy-of select="self::*/preceding-sibling::*[1]"/>
<xsl:apply-templates/>
</msub>
</xsl:template>


<xsl:template match="m:subsup">
<msubsup>
<xsl:copy-of select="self::*/preceding-sibling::*[1]"/>
<xsl:apply-templates/>
</msubsup>
</xsl:template>

<xsl:template match="m:sup">
<msup>
<xsl:copy-of select="self::*/preceding-sibling::*[1]"/>
<xsl:apply-templates/>
</msup>
</xsl:template>
</xsl:stylesheet>

所需输出:

<?xml version='1.0' ?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display="block">
<msup>
    <mfenced>
        <mrow>
            <mfrac>
                <mrow>
                    <msub><mi>v</mi><mrow><mi>n</mi></mrow></msub>
                </mrow>
                <mrow>
                    <msub><mi>v</mi><mrow><mi>d</mi></mrow></msub>
                </mrow>
            </mfrac>
        </mrow>
    </mfenced>
    <mrow><mn>2</mn></mrow>
</msup>
</math>
</chapter>

越南
vd
2.

当此XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns="http://www.w3.org/1998/Math/MathML"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:m="http://www.w3.org/1998/Math/MathML"
  exclude-result-prefixes="m" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="m:mfenced">
    <msup>
      <mfenced>
        <xsl:apply-templates />
      </mfenced>
      <xsl:apply-templates select="following-sibling::m:sup/*" />
    </msup>
  </xsl:template>

  <xsl:template match="m:mfrac/m:mrow">
    <mrow>
      <msub>
        <xsl:apply-templates />
      </msub>
    </mrow>
  </xsl:template>

  <xsl:template match="m:sub">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="m:sup" />
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
  <math display="block">
    <mfenced>
      <mrow>
        <mfrac>
          <mrow>
            <mi>v</mi>
            <sub>
              <mrow>
                <mi>n</mi>
              </mrow>
            </sub>
          </mrow>
          <mrow>
            <mi>v</mi>
            <sub>
              <mrow>
                <mi>d</mi>
              </mrow>
            </sub>
          </mrow>
        </mfrac>
      </mrow>
    </mfenced>
    <sup>
      <mrow>
        <mn>2</mn>
      </mrow>
    </sup>
  </math>
</chapter>
<?xml version="1.0"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
  <math display="block">
    <msup>
      <mfenced>
        <mrow>
          <mfrac>
            <mrow>
              <msub>
                <mi>v</mi>
                <mrow>
                  <mi>n</mi>
                </mrow>
              </msub>
            </mrow>
            <mrow>
              <msub>
                <mi>v</mi>
                <mrow>
                  <mi>d</mi>
                </mrow>
              </msub>
            </mrow>
          </mfrac>
        </mrow>
      </mfenced>
      <mrow>
        <mn>2</mn>
      </mrow>
    </msup>
  </math>
</chapter>

…应用于提供的XML:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns="http://www.w3.org/1998/Math/MathML"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:m="http://www.w3.org/1998/Math/MathML"
  exclude-result-prefixes="m" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="m:mfenced">
    <msup>
      <mfenced>
        <xsl:apply-templates />
      </mfenced>
      <xsl:apply-templates select="following-sibling::m:sup/*" />
    </msup>
  </xsl:template>

  <xsl:template match="m:mfrac/m:mrow">
    <mrow>
      <msub>
        <xsl:apply-templates />
      </msub>
    </mrow>
  </xsl:template>

  <xsl:template match="m:sub">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="m:sup" />
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
  <math display="block">
    <mfenced>
      <mrow>
        <mfrac>
          <mrow>
            <mi>v</mi>
            <sub>
              <mrow>
                <mi>n</mi>
              </mrow>
            </sub>
          </mrow>
          <mrow>
            <mi>v</mi>
            <sub>
              <mrow>
                <mi>d</mi>
              </mrow>
            </sub>
          </mrow>
        </mfrac>
      </mrow>
    </mfenced>
    <sup>
      <mrow>
        <mn>2</mn>
      </mrow>
    </sup>
  </math>
</chapter>
<?xml version="1.0"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
  <math display="block">
    <msup>
      <mfenced>
        <mrow>
          <mfrac>
            <mrow>
              <msub>
                <mi>v</mi>
                <mrow>
                  <mi>n</mi>
                </mrow>
              </msub>
            </mrow>
            <mrow>
              <msub>
                <mi>v</mi>
                <mrow>
                  <mi>d</mi>
                </mrow>
              </msub>
            </mrow>
          </mfrac>
        </mrow>
      </mfenced>
      <mrow>
        <mn>2</mn>
      </mrow>
    </msup>
  </math>
</chapter>

v
N
v
D
2.
…生成所需结果:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns="http://www.w3.org/1998/Math/MathML"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:m="http://www.w3.org/1998/Math/MathML"
  exclude-result-prefixes="m" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="m:mfenced">
    <msup>
      <mfenced>
        <xsl:apply-templates />
      </mfenced>
      <xsl:apply-templates select="following-sibling::m:sup/*" />
    </msup>
  </xsl:template>

  <xsl:template match="m:mfrac/m:mrow">
    <mrow>
      <msub>
        <xsl:apply-templates />
      </msub>
    </mrow>
  </xsl:template>

  <xsl:template match="m:sub">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="m:sup" />
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
  <math display="block">
    <mfenced>
      <mrow>
        <mfrac>
          <mrow>
            <mi>v</mi>
            <sub>
              <mrow>
                <mi>n</mi>
              </mrow>
            </sub>
          </mrow>
          <mrow>
            <mi>v</mi>
            <sub>
              <mrow>
                <mi>d</mi>
              </mrow>
            </sub>
          </mrow>
        </mfrac>
      </mrow>
    </mfenced>
    <sup>
      <mrow>
        <mn>2</mn>
      </mrow>
    </sup>
  </math>
</chapter>
<?xml version="1.0"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
  <math display="block">
    <msup>
      <mfenced>
        <mrow>
          <mfrac>
            <mrow>
              <msub>
                <mi>v</mi>
                <mrow>
                  <mi>n</mi>
                </mrow>
              </msub>
            </mrow>
            <mrow>
              <msub>
                <mi>v</mi>
                <mrow>
                  <mi>d</mi>
                </mrow>
              </msub>
            </mrow>
          </mfrac>
        </mrow>
      </mfenced>
      <mrow>
        <mn>2</mn>
      </mrow>
    </msup>
  </math>
</chapter>

v
N
v
D
2.

如果我理解正确,你想做的就是

  • 对于任何
    sub
    sup
    元素,将其上一个同级元素(如果有)移动到元素内,然后
  • sub
    重命名为
    msub
    并将
    sup
    重命名为
    msup
  • 这个XSLT应该做到以下几点:

    <?xml version='1.0' encoding='UTF-8' ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns="http://www.w3.org/1998/Math/MathML" exclude-result-prefixes="m">
      <xsl:output method="xml"/>
    
      <xsl:template match="*[following-sibling::*[1]/self::m:sub]">
        <msub>
          <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
          </xsl:copy>
          <xsl:apply-templates select="following-sibling::*[1]/self::m:sub/@* | following-sibling::*[1]/self::m:sub/node()"/>
        </msub>
      </xsl:template>
    
      <xsl:template match="*[following-sibling::*[1]/self::m:sup]">
        <msup>
          <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
          </xsl:copy>
          <xsl:apply-templates select="following-sibling::*[1]/self::m:sup/@* | following-sibling::m:sup/node()"/>
        </msup>
      </xsl:template>
    
      <xsl:template match="m:sub | m:sup"></xsl:template>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    
    
    

    我唯一不清楚的是,如何处理没有前一个同级的
    sub
    sup
    )元素。上面的代码只是忽略了它们。

    Siva,您可以通过引入新模板来阻止原始“mi”输出:


    请将您的答案编辑为:
    ,同样,m:sup的答案也是如此