简体   繁体   English

为什么减法和除法会在XSLT中给我NaN?

[英]Why are subtractions and divisions giving me NaN in XSLT?

So, I'm trying to make a symbolic derivative calculator and it works with a single hitch. 因此,我正在尝试制作一个符号导数计算器,并且它可以轻松实现。 It gives me NaN with this XML 它给了我NaN这个XML

<?xml version="1.0"?>
<sum>
    <mono>  
        <arg>3</arg>
        <var>x</var>
        <exp>2</exp>
    </mono>
</sum>

And this stylesheet that gives the numbers the behaviour they should have in a derivative. 这个样式表给出了数字在导数中应该具有的行为。

<xsl:stylesheet version="1.0"        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match = "/">
    <mono>
        <arg>
            <xsl:value-of select="arg div exp"></xsl:value-of>
        </arg>
    <var>
        <xsl:if test="exp > 0"/>
        X
    </var>
    <exp>
        <xsl:value-of select="exp - 1"></xsl:value-of>
    </exp>
</mono>
</xsl:template>
</xsl:stylesheet>

Your main template is matching the root element: 您的主模板与根元素匹配:

<xsl:template match = "/">

Change it to match mono elements instead: 更改它以匹配mono元素:

<xsl:template match = "mono">

Then your NaN issue will go away because the arg and exp elements will really be children of the current mono element now, whereas they were not children of the / element before. 然后,您的NaN问题将消失,因为argexp元素现在实际上实际上是当前mono元素的子元素,而它们以前不是/元素的子元素。

Here's your XSLT with the above a few other improvements: 这是您的XSLT,上面还有其他一些改进:

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

  <xsl:template match="mono">
    <mono>
      <arg>
        <xsl:value-of select="arg div exp"></xsl:value-of>
      </arg>
      <var>
        <xsl:if test="exp > 0">X</xsl:if>
      </var>
      <exp>
        <xsl:value-of select="exp - 1"/>
      </exp>
    </mono>
  </xsl:template>
</xsl:stylesheet>

Here's the XML output, without the NaNs, as requested: 这是根据要求的不带NaN的XML输出:

<?xml version="1.0" encoding="UTF-8"?>
<mono>
   <arg>1.5</arg>
   <var>X</var>
   <exp>1</exp>
</mono>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM