XQuery nested return flower

不羁的心 提交于 2019-12-04 12:23:54

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>

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!