Bernhard Auinger
Bernhard Auinger

Reputation: 13

xQuery: Always the same error: Brackets are not closed

I want to transform this XML-Document (factbook.xml) to HTML using xQuery.
But the code (can be found under the XML below) doesn't work.

I get always the same Error: (I work with BaseX 9.4.2)

Error:
Stopped at C:/Program Files (x86)/BaseX/etc/file2, 9/6:
[XPST0003] Expecting '}', found '{'.

I don't understand why I'm always getting the same error, all brackets are closed.

This is my XML-file: (Part)

<mondial>
 <continent id="f0_119" name="Europe"/>
 <continent id="f0_123" name="Asia"/>
 ...
<country id="f0_136" name="Albania"
 capital="f0_1461" population="3249136" total_area="28750">
 <city id="f0_1461" country="f0_136">
 <name>Tirane</name>
 <population>192000</population>
 </city>
 ...
 <encompassed continent="f0_119" percentage="100"/>
 </country>
<country id="f0_149" name="Austria"
 capital="f0_1467" population="8023244" total_area="83850">
 <province id="f0_17448" name="Upper Austria"
 capital="f0_2267" population="1373000">
 <city id="f0_2267">
 <name>Linz</name>
 <population>203000</population>
 </city>
 </province>
 <province id="f0_17447" name="Vienna"
 capital="f0_1467" population="1583000">
 <city id="f0_1467">
 <name>Vienna</name>
 <population>1583000</population>
 </city>
 </province>
 ...
 <encompassed continent="f0_119" percentage="100"/>
 </country>
 ...
<country id="f0_670" name="Turkey"
 capital="f0_1797" population="62484480" total_area="780580">
 <province id="f0_19040" name="Ankara"
 capital="f0_1797" population="3236626">
 <city id="f0_1797">
 <name>Ankara</name>
 <population>2782200</population>
 </city>
 </province>
 ...
 <encompassed continent="f0_119" percentage="32"/>
 <encompassed continent="f0_123" percentage="68"/>
 </country>
</mondial>

This is my xQuery-Code:

let $db := /mondial
let $html := 
<html>
<body>
{
  for $cont in $db/continent
  order by $cont/@name
  return <h1>""{$cont/@name}</h1> 
     {
      for $coun in $db/country
      order by $coun/@name
      where $cont/@id = $count//encompassed/@continent
      return <p>
         <h2>{coun/@name}</h2>
         <b>Capital:</b>
         {
           for $cap in $coun//city 
           where $coun/@capital = $cap/@id
           return $cap/name
         }
         <b>Total Area "in" {$cont/@name}:</b>
         {
           for $area in $coun//encompassed[@continent = $cont/@id]/percentage
           return data($coun/@total_area) * ($area div 100)
         }    
      </p>
    }
} </body> </html>
return $html

Thanks in advance for your help

Upvotes: 0

Views: 248

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

Saxon reports the same error:

Analyzing query from test.xq
Static error near {.../@name}</h1> { for} on line 8 at column 6 of file:/***/test.xq 
  XPST0003  expected "}", found "{"
Static error(s) in query

and I have to confess it's not immediately obvious. But the problem is the "{" that immediately follows return <h1>""{$cont/@name}</h1> . I think (judging from the indentation) you probably want this block to be part of the return clause, in which case it needs to be

let $db := /mondial
let $html := 
<html>
<body>
{
  for $cont in $db/continent
  order by $cont/@name
  return (<h1>""{$cont/@name}</h1>, 
      for $coun in $db/country
      order by $coun/@name
      where $cont/@id = $count//encompassed/@continent
      return <p>
         <h2>{coun/@name}</h2>
         <b>Capital:</b>
         {
           for $cap in $coun//city 
           where $coun/@capital = $cap/@id
           return $cap/name
         }
         <b>Total Area "in" {$cont/@name}:</b>
         {
           for $area in $coun//encompassed[@continent = $cont/@id]/percentage
           return data($coun/@total_area) * ($area div 100)
         }    
      </p>
    )
} </body> </html>
return $html

I then get an error for an unresolved reference to variable $count, but I assume that's a typo.

There are two common errors here:

  • An element constructor (like <x/>) is a valid XQuery expression, but a sequence of element constructors (like <x/><y/>) isn't: it needs a comma (<x/>,<y/>).
  • If you put an expression containing a comma operator after return, it needs parentheses around it.

The other error is less common: the construct "{" expr "}" is not a valid expression. Curly braces can only be used in quite specific places, there's no general curly-brace-delimited-block expression that can be used anywhere.

Upvotes: 2

Related Questions