3

我被一个看似简单的 XQuery 练习困住了。下面是一段 XML 文件。

<Ships>
<Class name = "Kongo" type = "bc" country = "Japan" numGuns = "8" 
        bore = "14" displacement = "32000">
    <Ship name = "Kongo" launched = "1913" />
    <Ship name = "Hiei" launched = "1914" />
    <Ship name = "Kirishima" launched = "1915">
        <Battle outcome = "sunk">Guadalcanal</Battle>
    </Ship>
    <Ship name = "Haruna" launched = "1915" />
</Class>
</Ships>

我正在尝试将 XML 转换为带有类名称属性 (Kongo) 作为标题和子船名称和发射年份的枚举列表的 XHTML:

<h1>Kongo</h1>
<table>
<tr><th>Name</th><th>Launched</th><tr>
<tr><td>Kongo</td><td>1913</td></tr>
<tr><td>Hiei</td><td>1914</td></tr>
<tr><td>Kirishima</td><td>1915</td></tr>
<tr><td>Haruna</td><td>1915</td></tr>

<h1>Next Class name</h1>
....

我坚持使用以下嵌套的 return-FLOWR XQuery:

declare option output "method=xml";
declare option output "doctype-public=-//W3C//DTD XHTML 1.0 Strict//EN";
declare option output "doctype-system=http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
declare option output "omit-xml-declaration=no";
declare option output "indent=yes";

<html xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>XQ-7</title>
  </head>
  <body>
       {for $class in doc("xml/battleships.xml")/Ships/Class 
    let $cname := data($class/@name) 
    let $sname := data($class/Ship/@name)
    let $slaunched := data($class/Ship/@launched)
      return   
      <h1>{$cname}</h1>
      <table>
      {for $ship in doc("xmlbattleships.xml")/Ships/Class/Ship
      where data($ship/../@name) eq $cname
        let $sname := data($ship/@name)
        let $slaunched := data($ship/@launched)
        return
         <tr><td>{$sname}</td><td>{$slaunched}</td></tr>
  }
</table>
    }
   </body>
</html>
4

2 回答 2

5

我认为你遇到的问题是 return 需要一个元素或一个序列,而你想同时返回 the<h1>和一个<table>元素。在这里使用一个序列:

return
(
    <h1>{$cname}</h1>,
    <table>
    [snip]
)

注意标题标签后的括号和冒号。您的子句还有另一个问题where,我将其移入谓词。

您想要的输出不是有效的 XHTML:您必须在表头之前关闭表格。您还将获得一个表格而不是枚举,您到底在寻找什么?

这个清理和修复的代码对我有用,我删除了您的文档和一些声明,但您将能够找到更改:

<html xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>XQ-7</title>
    </head>
    <body>
        {
            for $class in /Ships/Class 
            let $cname := data($class/@name) 
            let $sname := data($class/Ship/@name)
            let $slaunched := data($class/Ship/@launched)
            return
                (
                    <h1>{$cname}</h1>,
                    <table>
                    {
                        for $ship in /Ships/Class/Ship[@name = $cname]
                        let $sname := data($ship/@name)
                        let $slaunched := data($ship/@launched)
                        return
                        <tr>
                            <td>{$sname}</td>
                            <td>{$slaunched}</td>
                        </tr>
                    }
                    </table>
                )
        }
   </body>
</html>
于 2012-07-09T23:35:02.663 回答
2

这一点终于成功了,在其他地方进行了调节并进行了细微的调整。Xquery,你一定会喜欢它;):P。

    declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
    declare option output:method "xml";

    <html xml:lang="en">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>XQ-7</title>
        </head>
        <body>
            {
                for $class in doc("xml/battleships.xml")/Ships/Class 
                let $cname := data($class/@name) 
                let $sname := data($class/Ship/@name)
                let $slaunched := data($class/Ship/@launched)
                return
                    (
                        <h1>{$cname}</h1>,
                        <table>
                        <tr><th>Name</th><th>Launched</th></tr>
                        {
                            for $ship in doc("xml/battleships.xml")/Ships/Class/Ship[../@name = $cname]
                            let $sname := data($ship/@name)
                            let $slaunched := data($ship/@launched)
                            return
                            <tr>    
                                <td>{$sname}</td>
                                <td>{$slaunched}</td>
                            </tr>
                        }
                        </table>
                    )
            }
       </body>
    </html>
于 2012-07-10T07:59:42.357 回答