这个 xquery 脚本中的语法错误是什么?

发布于 2024-09-06 03:33:56 字数 2180 浏览 8 评论 0原文

我是 XQuery 的初学者,尝试做一些简单的练习来学习它。但是我试图组合在一起的这个最新查询拒绝运行,给我一个语法错误。

这是我的 XQuery:

<HTML>
<HEAD><TITLE>Alphabetical Cities Lists</TITLE></HEAD>
<BODY>
{
  for $indcode in (65 to 70)
  let $indlet := codepoints-to-string($indcode)
  return 
     <H1> {$indlet} LIST </H1>
     {
       for $cit in doc("mydoc.xml")/CITIES/ENTITY
       where starts-with($cit/NAME,$indlet)
       order by $cit/NAME
       return <LI>{$cit/NAME}</LI>
     }
     </OL>
} 
</BODY>
</HTML>

这是我的 XML 文件 mydoc.xml 的子集:

<CITIES>
  <ENTITY>
    <NAME>Hastings</NAME>
    <CITYCOD>230</CITYCOD>
    <CNTYCOD>01</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Tilden</NAME>
    <CITYCOD>487</CITYCOD>
    <CNTYCOD>02</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Alliance</NAME>
    <CITYCOD>008</CITYCOD>
    <CNTYCOD>07</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Hemingford</NAME>
    <CITYCOD>236</CITYCOD>
    <CNTYCOD>07</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Ainsworth</NAME>
    <CITYCOD>003</CITYCOD>
    <CNTYCOD>09</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Kearney</NAME>
    <CITYCOD>269</CITYCOD>
    <CNTYCOD>10</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Oakland</NAME>
    <CITYCOD>358</CITYCOD>
    <CNTYCOD>11</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Eagle</NAME>
    <CITYCOD>159</CITYCOD>
    <CNTYCOD>13</CNTYCOD>
  </ENTITY>
</CITIES>

我想要它做的是打印出一个简单的 HTML 文档,其中包含这些城市名称的多个列表,按它们开头的字母进行划分(A 到F)。

但是当我尝试在 此网站 上运行它时,它给了我这个错误:

查询:<>,第 9 行,第 6 列:[XPST0003] 语法错误,意外的“'{'”,期待“'}'”

我不知道为什么这么说。我检查了又检查,但我的大括号似乎都正确匹配。谁能看出问题是什么吗?谢谢。

I'm a beginner to XQuery, trying to do some simple exercises to learn it. But this latest query I'm trying to put together refuses to run, giving me a syntax error.

This is my XQuery:

<HTML>
<HEAD><TITLE>Alphabetical Cities Lists</TITLE></HEAD>
<BODY>
{
  for $indcode in (65 to 70)
  let $indlet := codepoints-to-string($indcode)
  return 
     <H1> {$indlet} LIST </H1>
     {
       for $cit in doc("mydoc.xml")/CITIES/ENTITY
       where starts-with($cit/NAME,$indlet)
       order by $cit/NAME
       return <LI>{$cit/NAME}</LI>
     }
     </OL>
} 
</BODY>
</HTML>

And this is a subset of my XML file mydoc.xml:

<CITIES>
  <ENTITY>
    <NAME>Hastings</NAME>
    <CITYCOD>230</CITYCOD>
    <CNTYCOD>01</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Tilden</NAME>
    <CITYCOD>487</CITYCOD>
    <CNTYCOD>02</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Alliance</NAME>
    <CITYCOD>008</CITYCOD>
    <CNTYCOD>07</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Hemingford</NAME>
    <CITYCOD>236</CITYCOD>
    <CNTYCOD>07</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Ainsworth</NAME>
    <CITYCOD>003</CITYCOD>
    <CNTYCOD>09</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Kearney</NAME>
    <CITYCOD>269</CITYCOD>
    <CNTYCOD>10</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Oakland</NAME>
    <CITYCOD>358</CITYCOD>
    <CNTYCOD>11</CNTYCOD>
  </ENTITY>
  <ENTITY>
    <NAME>Eagle</NAME>
    <CITYCOD>159</CITYCOD>
    <CNTYCOD>13</CNTYCOD>
  </ENTITY>
</CITIES>

What I want it to do is print out a simple HTML document with several lists of these city names, broken out by the letter they start with (A to F).

But when I try running this out on this site, it gives me this error:

Query: <>, line 9, column 6: [XPST0003] syntax error, unexpected "'{'", expecting "'}'"

I have no idea why it says that. I've checked and checked, but my curly braces all seem properly matched. Can anyone see what the problem is? Thanks.

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

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

发布评论

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

评论(2

笙痞 2024-09-13 03:33:56

这是一个常见的错误。

您需要在

    节点之间放置一个 , 。

单个节点是一个 XQuery 表达式(直接元素构造函数内的多个节点也是如此)。返回表达式必须是一个表达式。在这种情况下,您想要返回两个节点。每个节点都是一个单独的项目,因此为了返回它们,您需要按顺序连接两个节点。

查询如下所示:

<HTML>
<HEAD><TITLE>Alphabetical Cities Lists</TITLE></HEAD>
<BODY>
{
  for $indcode in (65 to 70)
  let $indlet := codepoints-to-string($indcode)
  return 
     (<H1> {$indlet} LIST </H1>,
     <OL>
     {
       for $cit in doc("mydoc.xml")/CITIES/ENTITY
       where starts-with($cit/NAME,$indlet)
       order by $cit/NAME
       return <LI>{$cit/NAME}</LI>
     }
     </OL>)
} 
</BODY>
</HTML>

This is a common mistake.

You need to place a , between the <H1> and <OL> nodes.

A single node is an XQuery expression (as are multiple nodes inside a direct element constructor). The return expression must be an expression. In this case you want to return two nodes. Each node is a single item and so in order to return them both you need to concatenate the two nodes in a sequence.

The query would look like this:

<HTML>
<HEAD><TITLE>Alphabetical Cities Lists</TITLE></HEAD>
<BODY>
{
  for $indcode in (65 to 70)
  let $indlet := codepoints-to-string($indcode)
  return 
     (<H1> {$indlet} LIST </H1>,
     <OL>
     {
       for $cit in doc("mydoc.xml")/CITIES/ENTITY
       where starts-with($cit/NAME,$indlet)
       order by $cit/NAME
       return <LI>{$cit/NAME}</LI>
     }
     </OL>)
} 
</BODY>
</HTML>
山有枢 2024-09-13 03:33:56

您忘记了

    ;在第 9 行的 { 之前。

所以应该是:

<HTML>
<HEAD><TITLE>Alphabetical Cities Lists</TITLE></HEAD>
<BODY>
{
  for $indcode in (65 to 70)
  let $indlet := codepoints-to-string($indcode)
  return 
     <H1> {$indlet} LIST </H1>
     <OL>
     {
       for $cit in doc("mydoc.xml")/CITIES/ENTITY
       where starts-with($cit/NAME,$indlet)
       order by $cit/NAME
       return <LI>{$cit/NAME}</LI>
     }
     </OL>
} 
</BODY>
</HTML>

You forgot a <OL> before the { on line 9.

So it should be:

<HTML>
<HEAD><TITLE>Alphabetical Cities Lists</TITLE></HEAD>
<BODY>
{
  for $indcode in (65 to 70)
  let $indlet := codepoints-to-string($indcode)
  return 
     <H1> {$indlet} LIST </H1>
     <OL>
     {
       for $cit in doc("mydoc.xml")/CITIES/ENTITY
       where starts-with($cit/NAME,$indlet)
       order by $cit/NAME
       return <LI>{$cit/NAME}</LI>
     }
     </OL>
} 
</BODY>
</HTML>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文