返回多元素 HTML 作为 xquery 结果

发布于 2024-10-17 18:50:03 字数 790 浏览 4 评论 0原文

我在返回多元素 HTML 结果时遇到问题(我不知道这个结果的名称)。为了更清楚地说明这一点,请参阅下面的代码。

<body>
  <h1>Nobel prize winners</h1>{
  for $subject in distinct-values(doc("nobel.xml")//subject)
    let $sn := $subject`
    return <div><h2>{$sn}</h2><ul>
       {for $row in doc("nobel.xml")/nobel/row
          let $name := $row/winner/text()
          let $year := $row/yr/text()
          where $row/subject/text()=$subject
          order by $year
          return <li> ({$year} {$name} </li>
        }</ul></div>
  }
</body>

该代码可以工作,但您可以注意到我必须将

放在 的开头和结尾return 部分,这是我实际上不想要的。我的问题是,有什么办法可以去掉吗?我尝试过 { }( ) ,但两者都不起作用。

I have a problem with returning multi-element HTML result (I dont know the name for this). To make it clearer, please see the code below.

<body>
  <h1>Nobel prize winners</h1>{
  for $subject in distinct-values(doc("nobel.xml")//subject)
    let $sn := $subject`
    return <div><h2>{$sn}</h2><ul>
       {for $row in doc("nobel.xml")/nobel/row
          let $name := $row/winner/text()
          let $year := $row/yr/text()
          where $row/subject/text()=$subject
          order by $year
          return <li> ({$year} {$name} </li>
        }</ul></div>
  }
</body>

That code will work, but you can notice that I have to put that <div> and </div> in the beginning and the end of the return part, which I actually don't want. My question is, is there any way to remove it? I have tried with { } and ( ) and both don't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-10-24 18:50:03

以下是 XQuery 1.0 语法的语法规则,取自 XQuery 1.0 w3c 规范

[31]    Expr          ::=    ExprSingle ("," ExprSingle)* 
[32]    ExprSingle    ::=    FLWORExpr
                           | QuantifiedExpr
                           | TypeswitchExpr
                           | IfExpr
                           | OrExpr 
[33]    FLWORExpr    ::=    (ForClause | LetClause)+ WhereClause? 
                                             OrderByClause? "return" ExprSingle 

可以清楚地看到,return 标记(关键字)后面必须跟有单个表达式。

但是,通过删除 div 元素,您拥有

<body>
    <h1>Nobel prize winners</h1>
    {
     for $subject in
           distinct-values(doc("nobel.xml")//subject)
      let $sn := $subject
        return
            <h2>{$sn}</h2>
                <ul>
                  {for $row in doc("nobel.xml")/nobel/row
                    let $name := $row/winner/text()
                    let $year := $row/yr/text()
                    where $row/subject/text()=$subject
                    order by $year
                       return
                         <li> ({$year} {$name} </li>
                  }
                </ul>
        }
</body>

正如我们可以清楚地看到的,return 关键字后面没有跟随单个表达式,由 XQuery 1.0 语法定义。

解决方案:明确指定 return 关键字后面的元素序列,作为序列:

<body>
    <h1>Nobel prize winners</h1>
    {
     for $subject in
           distinct-values(doc("nobel.xml")//subject)
      let $sn := $subject
        return
          (
            <h2>{$sn}</h2>,
                <ul>
                  {for $row in doc("nobel.xml")/nobel/row
                    let $name := $row/winner/text()
                    let $year := $row/yr/text()
                    where $row/subject/text()=$subject
                    order by $year
                       return
                         <li> ({$year} {$name} </li>
                  }
                </ul>
          )
        }
</body>

注意return后面的元素序列 现在被括号包围,并且序列中的元素由逗号分隔。

现在,当上面的 XQuery 代码在此 nobel.xml 文件上执行时

<nobel>
 <row>
  <subject>Lit</subject>
  <winner>Solzhenitsin</winner>
  <yr>1969</yr>
 </row>
</nobel>

生成所需的正确结果:

<body>
    <h1>Nobel prize winners</h1>
    <h2>Lit</h2>
    <ul>
        <li> (1969Solzhenitsin</li>
    </ul>
</body>

Here are the syntax rule for the grammar of XQuery 1.0 , as taken from the XQuery 1.0 w3c spec.:

[31]    Expr          ::=    ExprSingle ("," ExprSingle)* 
[32]    ExprSingle    ::=    FLWORExpr
                           | QuantifiedExpr
                           | TypeswitchExpr
                           | IfExpr
                           | OrExpr 
[33]    FLWORExpr    ::=    (ForClause | LetClause)+ WhereClause? 
                                             OrderByClause? "return" ExprSingle 

As can be clearly seen, the return token (keyword) must be followed by a single expression.

However, by getting rid of the div element you have:

<body>
    <h1>Nobel prize winners</h1>
    {
     for $subject in
           distinct-values(doc("nobel.xml")//subject)
      let $sn := $subject
        return
            <h2>{$sn}</h2>
                <ul>
                  {for $row in doc("nobel.xml")/nobel/row
                    let $name := $row/winner/text()
                    let $year := $row/yr/text()
                    where $row/subject/text()=$subject
                    order by $year
                       return
                         <li> ({$year} {$name} </li>
                  }
                </ul>
        }
</body>

As we can clearly see, the return keyword is not followed by a single expression, as defined by the XQuery 1.0 grammar.

Solution: Clearly specify the sequence of elements that follow the return keyword, as a sequence:

<body>
    <h1>Nobel prize winners</h1>
    {
     for $subject in
           distinct-values(doc("nobel.xml")//subject)
      let $sn := $subject
        return
          (
            <h2>{$sn}</h2>,
                <ul>
                  {for $row in doc("nobel.xml")/nobel/row
                    let $name := $row/winner/text()
                    let $year := $row/yr/text()
                    where $row/subject/text()=$subject
                    order by $year
                       return
                         <li> ({$year} {$name} </li>
                  }
                </ul>
          )
        }
</body>

Notice that the sequence of elements following return is now surrounded by brackets, and that the elements in the sequence are delimited by a comma.

Now, when the above XQuery code is executed on this nobel.xml file:

<nobel>
 <row>
  <subject>Lit</subject>
  <winner>Solzhenitsin</winner>
  <yr>1969</yr>
 </row>
</nobel>

the wanted, correct result is produced:

<body>
    <h1>Nobel prize winners</h1>
    <h2>Lit</h2>
    <ul>
        <li> (1969Solzhenitsin</li>
    </ul>
</body>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文