XQuery if语句

时间:2013-09-09 15:34:30

标签: xquery

我遇到XQuery问题。我花了很多时间来处理这个问题,我无法让它工作:(这是我的XML书籍节点:

   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>

这就是我想要实现的目标:

<table>
   <tr>
      <td>
         <b>XML Developer's Guide</b>
      </td>
      <td class="author">Gambardella, Matthew</td>
      <td class="genre">Computer</td>
      <td class="price">44.95</td>
      <td class="priceVat">55.29</td>
      <td class="publish_date">2000-10-01</td>
      <td class="description">An in-depth look at creating applications 
      with XML.</td>
   </tr>
</table>

这是我的XQuery代码:

for $book in doc("/home/kuba/mgr/pliki/books.xml")//book
order by number($book/price) descending
   return
     <tr>
       <td><b>{ string($book/title) }</b></td>
       {
         for $value in $book/*
         return if($value != $book/title and name($value) != 'price') then
            <td class="{name($value)}">{$value/text()}</td>
           else
            if(name($value) = 'price') then
                <td class="{name($value)}">{$value/text()}</td>
                <td class="{name($value)}Vat">{$value/text()*1.23}</td>
            else ()
       }
     </tr>

问题出在这一行:

        <td class="{name($value)}Vat">{$value/text()*1.23}</td>

这是我的错误消息:

Engine name: Saxon-PE XQuery 9.5.0.2
Severity: fatal
Description: XPST0003: expected "else", found name "class"
Start location: 17:0
URL: http://www.w3.org/TR/xpath20/#ERRXPST0003

我怎么做到的?可以在XQuery 1.0中格式化数字,我读到这是不可能的,但我想确保:)

1 个答案:

答案 0 :(得分:2)

在你的then子句之后,你返回一系列<td>个元素,但是它们需要用括号括起来并用逗号分隔:

if(name($value) = 'price') then
  (<td class="{name($value)}">{$value/text()}</td>,
  <td class="{name($value)}Vat">{$value/text()*1.23}</td>)
else ()