tags:

views:

23

answers:

2

Hello,

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.

+1  A: 

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>
Johannes Jensen
Thanks, you're right, I did. But that still leaves the syntax error.
BigBeagle
+2  A: 

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>
Oliver Hallam
Thank you very much. This is exactly what I needed. And thanks for the detailed explanation.
BigBeagle