XQuery嵌套for子句

时间:2014-02-19 10:48:31

标签: xpath xquery

我一直在XQuery中收到此错误,但我确信我打开并关闭了括号。这个查询可能出了什么问题?

XPST0003 XQuery syntax error near #...{ $x/name }</name> {for#:
    expected "}", found "{"

这是查询

  <myquery>
        <A>

        {for $x in doc("example.xml")/example/A
        where $x/name = "United States" 
        return 
            <name>{ $x/name }</name> (: error reported at this line of code :)

            {for $y in $x/B
            let $z := $y/C div $y/D
            order by $z
            return
                <B>
                    <C>{$y/name/text()}</C>
                    <ratio>{ $z }</ratio>
                </B>
            }
        }

    </A>
</myquery>

1 个答案:

答案 0 :(得分:1)

如果您要返回名称标签后跟B标签,则必须return一个序列:

<myquery>
    <A>{
        for $x in doc("example.xml")/example/A
        where $x/name = "United States"
        return (                        (: Return a sequence here :)
            <name>{ $x/name }</name>,   (: Be aware of the comma :)

            for $y in $x/B
            let $z := $y/C div $y/D
            order by $z
            return
                <B>
                    <C>{$y/name/text()}</C>
                    <ratio>{ $z }</ratio>
                </B>
        )                               (: end of sequence :)
    }</A>
</myquery>

我还删除了FLWOR表达式周围不必要的大括号,并注意我在名称标记后面添加了一个逗号。