XQuery nested return flower

2020-07-22 18:58发布

问题:

I'm stuck with a seemingly simple XQuery exercise. Below a piece of the XML file.

<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>

I'm trying to transform the XML into an XHTML with the Class name attributes (Kongo) as header and an enumerated list of the child ship names and launch-years:

<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>
....

I'm stuck with the following nested 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>

回答1:

I think that the problem you're stuck with is that return needs a single element or a sequence while you want to return both the <h1> and also a <table> element. Use a sequence here:

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

Watch out for both the parentheses and also the colon after the headline tag. There was another problem with your where clause which I moved into a predicate.

Your desired output isn't valid XHTML: you will have to close the table before the header. You will also get a table instead of an enumeration, what are you looking fore exactly?

This cleaned up and fixed code works for me, I stripped of your document and some declarations, but you will be able to find the changes:

<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>


回答2:

This bit finally did the trick, with conditioning done elsewhere and minor tweaks. Xquery, you got to love it ;):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>