Here is a (minimal?) reproduction:


    let $n := <x/>
    for $p at $i in $n
    where $i = count($n[. = $p])
    return $p


The complete response is shown below. This is from 9.0.0.2J. Earlier
versions come up with an "ArrayIndexOutOfBoundsException: -999".
In 8.2 it seems to work OK.

Best regards,
Gunther



*** Internal Saxon error: Unbound local variable encountered
Variable name: p
Line number of reference: 3
Line number of declaration: 2
DECLARATION:
<for variable="p" as="element(x, {http://www.w3.org/2001/XMLSchema}untyped)">
  <in>
    <functionCall name="subsequence">
      <variableReference name="n"/>
      <operator op="+">
        <functionCall name="count">
          <let variable="zz:zz8146001" as="xs:string">
            <be>
              <lazy>
                <cast as="xs:string">
                  <atomize>
                    <variableReference name="p"/>
                  </atomize>
                </cast>
              </lazy>
            </be>
            <return>
              <filterExpression>
                <variableReference name="n"/>
                <operator op="eq">
                  <cast as="xs:string">
                    <atomize>
                      <dot/>
                    </atomize>
                  </cast>
                  <variableReference name="zz:zz8146001"/>
                </operator>
              </filterExpression>
            </return>
          </let>
        </functionCall>
        <literal value="1" type="xs:integer"/>
      </operator>
    </functionCall>
  </in>
  <return>
    <variableReference name="p"/>
  </return>
</for>java.lang.IllegalStateException: *** Internal Saxon error: Unbound local variable encountered
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:502)
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:507)
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:507)
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:507)
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:507)
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:507)
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:507)
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:507)
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:507)
        at net.sf.saxon.expr.ExpressionTool.allocateSlots(ExpressionTool.java:475)
        at net.sf.saxon.query.XQueryExpression.<init>(XQueryExpression.java:75)
        at net.sf.saxon.query.QueryParser.makeXQueryExpression(QueryParser.java:121)
        at net.sf.saxon.query.StaticQueryContext.compileQuery(StaticQueryContext.java:338)
        at net.sf.saxon.query.StaticQueryContext.compileQuery(StaticQueryContext.java:400)
        at net.sf.saxon.Query.doQuery(Query.java:522)
        at net.sf.saxon.Query.main(Query.java:91)
Fatal error during query: java.lang.IllegalStateException: *** Internal Saxon error: Unbound local variable encountered


 
Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt, Germany, – Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/ Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), David Broadbent, Mark Edwards, Dr. Peter Kürpick, David Mitchell, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/ Chairman of the Supervisory Board: Frank F. Beelitz - http://www.softwareag.com