XQuery返回不计算任何内容

时间:2018-07-18 16:39:26

标签: xml xquery saxon

因此,我们需要对XQuery进行此练习。用于存储在XML中的运算的计算器。这是我第一次使用XQuery,我非常困惑。无论我在return语句中输入什么内容,Saxon都不会返回包含在return段中的函数的结果,而只会以纯文本形式返回它,因此调用:

declare function m:evaluate($tree as element()) as element(fraction) {
if(local-name($tree)="fraction") then (

 for $a in $tree
  return
     <fraction>
        <numerator>$tree/numerator</numerator>
        <denominator>$tree/denominator </denominator>
     </fraction>
)
else(

typeswitch($tree)
  case $tree as element(sum)
    return element fraction{
      <numerator>$tree/*[1]/numerator/$tree/*[1]/denominator*$ggT+$tree/*[2]/numerator/$tree/*[2]/denominator*$ggT)</numerator>,
      <denominator>m:findggT($tree/*[1]/denominator,$tree/*[2]/denominator)</denominator>}
  case $tree as element(product)
    return element fraction{
      <numerator>m:evaluate($tree/*[1]/numerator)*m:evaluate($tree/*[2]/numerator)</numerator>,
      <denominator>m:evaluate($tree/*[1]/denominator)*m:evaluate($tree/*[2]/denominator)</denominator>
    }
  default return element fraction  {
      <numerator>$tree/numerator</numerator>,
      <denominator>$tree/denominator </denominator>
  }


)

返回

<fraction>
   <numerator>m:evaluate($tree/*[1]/numerator)*m:evaluate($tree/*[2]/numerator)</numerator>
   <denominator>m:evaluate($tree/*[1]/denominator)*m:evaluate($tree/*[2]/denominator)</denominator>
</fraction>%     

很明显,被调用xml的根元素是product

似乎我错过了一些有关XQuery中函数调用的工作方式,但我不知道是什么。

我使用的XML是:

<product>
  <sum>
    <fraction >
      <numerator >1</numerator >
      <denominator >2</denominator >
    </fraction >
    <fraction >
      <numerator >1</numerator >
      <denominator >3</denominator >
    </fraction >
    <fraction >
      <numerator >1</numerator >
      <denominator >4</denominator >
    </fraction >
  </sum>
  <fraction >
    <numerator >2</numerator >
    <denominator >3</denominator >
  </fraction >
</product >

1 个答案:

答案 0 :(得分:1)

您的表达式被视为文本,因为它们不在花括号({})内。大括号已经是computed element constructor语法的一部分,但是在使用direct element constuctor来区分纯文本和表达式时,需要添加大括号:

<numerator>{ m:evaluate($tree/*[1]/numerator) * m:evaluate($tree/*[2]/numerator)}</numerator>