4

我想使用 xslt 从 Mathml 输入中输出数学表达式。我的变换一定有什么错误。我不知道如何纠正它。你可以帮帮我吗?我应该使用参数还是变量来控制它?xml:

<?xml version="1.0" encoding="UTF-8"?>

<math>
  <apply>
    <eq/>
    <apply>
      <plus/>
      <apply>
        <power/>
        <ci>x</ci>
        <cn>2</cn>
      </apply>
      <apply>
        <times/>
        <cn>2</cn>
        <ci>x</ci>
      </apply>
      <cn>2</cn>
    </apply>
    <cn>0</cn>
  </apply>
</math>

这是我的 xsl:

  <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="text" encoding="UTF-8"/>

    <xsl:template match="/">
     <xsl:apply-templates/>
     <xsl:text>&#10;</xsl:text>
    </xsl:template>

    <xsl:template match="math">
            <xsl:apply-templates select="apply"/>
    </xsl:template>

    <xsl:template match="apply">

        <xsl:sequence select="name(element()[1]),'('"/>
        <xsl:apply-templates select="apply">
        </xsl:apply-templates>
        <xsl:sequence select="element()[2],','"/>
        <xsl:sequence select="element()[3],')',','"/>

   </xsl:template>

</xsl:stylesheet>

结果应该是:

   eq(plus(power(x,2),times(2,x),2),0)
4

2 回答 2

2

我稍微改变了你原来的变换以获得想要的结果。基本上,apply模板规则是决定函数父代的规则。为了确定是否插入逗号,我正在检查以下特定类型的兄弟元素。

此外,我已将您的替换xsl:sequence为,xsl:value-of从而防止创建不需要的空间(我想它们是不需要的)。功能element()也被替换了。样式表现在与 XSLT 1.0 兼容。我不喜欢 XSLT 1.0,但我更喜欢仅在真正需要时才使用新功能。

这个解决方案不是很通用(你知道 MATHML 是一个巨大的规范),但你可以使用它并使其适应更复杂的情况。


XSLT 1.0在Saxon 6.5.5下测试

  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="apply">
        <xsl:value-of select="concat(name(child::*[1]),'(')"/>
        <xsl:apply-templates select="apply|cn|ci"/>
        <xsl:value-of select="')'"/>
        <xsl:if test="count(following-sibling::*)">
            <xsl:value-of select="','"/>
        </xsl:if>
    </xsl:template>

    <xsl:template match="cn|ci">
        <xsl:value-of select="."/>
        <xsl:if test="count(following-sibling::*)&gt;0">
            <xsl:value-of select="','"/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

当应用于问题中显示的输入时,会产生:

eq(plus(power(x,2),times(2,x),2),0)
于 2011-05-21T21:33:40.627 回答
2

这个完整而短暂的转变

<xsl:stylesheet version="1.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="apply">
  <xsl:if test="not(position()=1)">,</xsl:if>
  <xsl:apply-templates select="*[1]"/>
 </xsl:template>

 <xsl:template match="eq|plus|power|times">
  <xsl:if test="not(position()=1)">,</xsl:if>
  <xsl:value-of select="concat(name(), '(')"/>
   <xsl:apply-templates select="following-sibling::*"/>
   <xsl:text>)</xsl:text>
 </xsl:template>

 <xsl:template match="cn|ci">
  <xsl:if test="not(position()=1)">,</xsl:if>
  <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<math>
    <apply>
        <eq/>
        <apply>
            <plus/>
            <apply>
                <power/>
                <ci>x</ci>
                <cn>2</cn>
            </apply>
            <apply>
                <times/>
                <cn>2</cn>
                <ci>x</ci>
            </apply>
            <cn>2</cn>
        </apply>
        <cn>0</cn>
    </apply>
</math>

产生想要的正确结果

eq(plus(power(x,2),times(2,x),2),0)
于 2011-05-21T22:41:38.823 回答