0

我需要移动输出中显示<sub><sup>元素。我可以移动<sup>元素。子元素没有移动。我还需要相应地重命名<msub>元素<msup>。如果我运行 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>

示例 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>
4

3 回答 3

0

当这个 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:

<?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>
于 2013-04-09T15:42:47.013 回答
0

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

  1. 对于任何subsup元素,将其先前的兄弟(如果有)移动到元素内,并且
  2. 重命名submsub和。supmsup

这个 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) 没有先前兄弟的元素。上面的代码只是忽略了它们。

于 2013-04-09T16:03:37.773 回答
0

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

<xsl:template match="m:mi"> <xsl:if test="not(name(following-sibling:: [1]) = 'sub') and not(name(following-sibling:: [1] ) = 'sup') 而不是(name(following-sibling::*[1]) = 'subsup')"> <mi> <xsl:apply-templates/> </mi> </xsl:if> < /xsl:模板>

于 2013-04-10T04:46:08.747 回答