为什么XQuery会返回除数而不是除法结果呢?

时间:2021-10-23 20:46:21

I have the following XML document:

我有以下XML文档:

<releases>
  <release>
    <videos>
      <video duration="599" embed="true" src="http://www.youtube.com/watch?v=TivSU1-ASPQ"/>
    </videos>
  </release>
</releases>

I would like to convert the @duration to minutes and seconds. I am using the following code to convert

我想将@duration转换为分钟和秒。我正在使用以下代码进行转换

declare namespace local = "http://www.foobar.org";
declare function local:convert($v) as xs:decimal?
{
  $v/60
};

let $r := doc("releases.xml") return
for $i in $r/releases/release/videos/video
return local:convert($i/@duration)

However, I am getting a 60 as the result. I don't understand what I am doing wrong.

然而,结果我得60分。我不明白我做错了什么。

PS: Please note that I have multiple videos, that is why I am using a FLWOR expression.

请注意,我有多个视频,这就是我使用FLWOR表达式的原因。

1 个解决方案

#1


5  

In XQuery, / does not denote the division operator, but an axis step. Use div instead. Your query returns the decimal 60 for each $v. You can also try (<foo/>, <bar/>)/42 for a similar result, which will return the number 42 twice.

在XQuery中,/不表示除法运算符,而是表示轴步骤。而是使用div。您的查询为每个$v返回decimal 60。您还可以尝试( )/42获得类似的结果,它将返回42次。

Additionally, that function will fail for incompatible types. The result of a division is an xs:double, not an xs:decimal (which is only for integers). Declare the function as returning xs:double instead (or cast/round to a decimal, if you want integers).

此外,该函数对于不兼容的类型将失败。除法的结果是xs:double,而不是xs:decimal(它只适用于整数)。将函数声明为返回xs:改为双精度(如果您想要整数,可以将其转换为小数)。

declare function local:convert($v) as xs:double?
{
  $v div 60
};

#1


5  

In XQuery, / does not denote the division operator, but an axis step. Use div instead. Your query returns the decimal 60 for each $v. You can also try (<foo/>, <bar/>)/42 for a similar result, which will return the number 42 twice.

在XQuery中,/不表示除法运算符,而是表示轴步骤。而是使用div。您的查询为每个$v返回decimal 60。您还可以尝试( )/42获得类似的结果,它将返回42次。

Additionally, that function will fail for incompatible types. The result of a division is an xs:double, not an xs:decimal (which is only for integers). Declare the function as returning xs:double instead (or cast/round to a decimal, if you want integers).

此外,该函数对于不兼容的类型将失败。除法的结果是xs:double,而不是xs:decimal(它只适用于整数)。将函数声明为返回xs:改为双精度(如果您想要整数,可以将其转换为小数)。

declare function local:convert($v) as xs:double?
{
  $v div 60
};