Xsl如何避免楠 [英] Xsl how to avoid nan

查看:68
本文介绍了Xsl如何避免楠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个包含数字的属性。但是可能没有定义其中一个或几个(甚至每个)(即在xml文件中没有这样的attr)。我需要显示attrs的总和,所以当至少其中一个未定义时我得到NaN。



那么我怎么能避免得到NaN?我的意思是我需要将NaN值解释为0,就像NaN + 10 + 20 + NaN + 30应该给我60作为结果,但不是NaN是否有任何方法可以避免无条件的NaN结果,就像JS中的var || 0如果未定义var,将返回0吗?



我尝试过:



xml元素的示例。我需要总结每个attr(attr3未定义)

< element attr1 =5attr2 =1/>



xsl sum的例子

< xsl:value-of select =@ attr1 + @ attr2 + @ attr3/>

解决方案

< BLOCKQUOTE>无。不要这样做。 NaN不为0,0不是NaN。假装一个是另一个意味着数据丢失。使用NaN;这是一个你无法理解的合法对象;这非常重要。



我希望你明白NaN不是一个数字。通过将0解释为不是数字,你将自己置于古代和早期中世纪人的位置,他们没有零或者没有将其视为数字。欢迎回到野蛮的世界!

(但是,这大致与美国国税局收取税款的人对数学和算术的理解程度大致相符。

如果你是其中之一,你可能需要一两个世纪,才能更接近欧洲文艺复兴时期的数学世界。:-))



也许你需要一些理解:

NaN - 维基百科,免费的百科全书 [ ^ ],

浮点 - 维基百科,免费的百科全书 [ ^ ],

IEEE浮点数 - 维基百科,免费的百科全书 [ ^ ]。



-SA


select =sum(@ attr1 | @ attr2 | @ attr3)


我知道这个问题已经有了一个公认的答案,但是当我看到这样的硬编码解决方案时,它总是让我感到畏缩。



最好总结所有现有属性,而不是使用他们的特定名称。

如果你想添加 attr4 attr5 以后?

然后你必须改变你的代码才能适应那种情况。



以下解决方案仅在您想获得所有现有属性的总和时才有效。



示例XML

< pre lang =xml> < root >
< element attr1 = 10 attr2 = 20 < span class =code-attribute> / >
< element attr1 = 10 attr2 = 20 attr3 = 30 / >
< element attr1 = 10 attr2 = < span class =code-keyword> 20 attr3 = 30 attr4 = 40 attr5 = 50 / >
< / root >





XSLT

 <   xsl:stylesheet     version   =  1.0    xmlns:xsl   =  http://www.w3.org/1999/XSL/Transform > ;  
xmlns:msxsl =urn:schemas-microsoft-com:xsltexclude-result-prefixes =msxsl
>
< xsl:output 方法 = text 缩进 = / >

< xsl:template 匹配 = root >
< xsl:apply-templates 选择 = element / >
< / xsl:template >

< xsl:template 匹配 < span class =code-keyword> = element >
属性Sum = < xsl:value-of 选择 = sum(@ *) / >
< / xsl:template >

< / xsl:stylesheet >





输出

属性Sum = 30 
属性Sum = 60
属性Sum = 150


I have several attributes which contain numbers. But one of them or several (or even every) may not be defined (i.e. no such attr in xml-file). I need to display sum of attrs, so when at least one of them is undefined I get NaN.

So how can I avoid getting NaN? I mean I need to interpret NaN value as 0, like NaN + 10 + 20 + NaN + 30 should give me 60 as result, but not NaN Is it any way to avoid NaN result without conditions, just like var||0 in JS will return 0 if var is undefined?

What I have tried:

Example of xml element. I need to sum every attr (attr3 is undefined)
<element attr1 = "5" attr2 = "1"/>

Example of xsl sum
<xsl:value-of select="@attr1 + @attr2 + @attr3"/>

解决方案

No. Don't do it. NaN is not 0, 0 is not NaN. Pretending one is another would mean loss of data. Use NaN; this is a legitimate object you just fail to understand; it is very important.

I hope you understand that NaN is "not a number". By interpreting 0 as not a number, you put yourself in the position of ancient and early Medieval people who did not have zero or did not consider it as a number. Welcome back to the barbaric world!
(However, this roughly matches the level of understanding of mathematics and arithmetic by the U.S. Internal Revenue Service, people who collect taxes.
If you are one of them, your will probably need a century or two, to get to the world of mathematics a bit closer to the time of European Renaissance. :-))

Perhaps you need some understanding:
NaN — Wikipedia, the free encyclopedia[^],
Floating point — Wikipedia, the free encyclopedia[^],
IEEE floating point — Wikipedia, the free encyclopedia[^].

—SA


select="sum(@attr1 | @attr2 | @attr3)"


I know this question already has an accepted answer, but it always makes me cringe when I see hard coded solutions like this.

Better to to try to sum up all existing attributes, than to use their specific names.
What if you want to add attr4 and attr5 later?
Then you have to change your code to work for that scenario.

The solution below only works if you want to get the sum of all existing attributes.

Example XML

<root>
    <element attr1="10" attr2="20" />
    <element attr1="10" attr2="20" attr3="30" />
    <element attr1="10" attr2="20" attr3="30" attr4="40" attr5="50" />
</root>



XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="text" indent="no" />

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

  <xsl:template match="element">
    Attribute Sum = <xsl:value-of select="sum(@*)" />
  </xsl:template>

</xsl:stylesheet>



Output

Attribute Sum = 30
Attribute Sum = 60
Attribute Sum = 150


这篇关于Xsl如何避免楠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆