【问题标题】:using xsl to output the math expression使用 xsl 输出数学表达式
【发布时间】:2011-05-21 16:57:16
【问题描述】:

我想使用 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)

【问题讨论】:

  • @empo,我想知道您的编辑是否应该包含在答案而不是问题中;感觉就像你知道自己在做什么 :) 但我不知道 为什么 你在做这些事情。 :)
  • @sarnold:它们应该是安全的编辑,可以明确问题。我已将缺少的代码部分添加到声明使用的版本的 xslt 中。我设置了 2.0 版本,因为 OP 使用的是 XSLT 2.0 的元素和功能。
  • @pst,1.0 在 @empo 的 first 编辑中,@ZAWD 的原始帖子未提供。 :)
  • 是的,抱歉大家做了一些更改而不是一个 :) 并感谢您接受它们。
  • 好问题,+1。请参阅我的答案以获得完整、最简单和最短的(目前)解决方案。 :)

标签: xslt mathml


【解决方案1】:

这个完整而短暂的转变

<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)

【讨论】:

【解决方案2】:

我稍微更改了您的原始转换以获得想要的结果。基本上,apply 模板规则是决定函数 parentesis 的规则。为了确定是否插入逗号,我正在检查以下特定类型的同级元素。

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

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


XSLT 1.0Saxon 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)

【讨论】:

    猜你喜欢
    • 2019-04-23
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 2021-01-27
    • 2010-11-12
    相关资源
    最近更新 更多