1

我对 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 中格式化数字,我读到这是不可能的,但我想确保 :)

4

1 回答 1

3

在您的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 ()
于 2013-09-09T15:42:18.040 回答