private void generateQvarConstraints ( ) { final StringBuilder qvarConstrBuilder = new StringBuilder ( ) ; final StringBuilder qvarMapStrBuilder = new StringBuilder ( ) ; final Iterator < Map . Entry < String , ArrayList < String > > > entryIterator = qvar . entrySet ( ) . iterator ( ) ; if ( entryIterator . hasNext ( ) ) { qvarMapStrBuilder . append ( "declare function local:qvarMap($x) {\n map {" ) ; while ( entryIterator . hasNext ( ) ) { final Map . Entry < String , ArrayList < String > > currentEntry = entryIterator . next ( ) ; final Iterator < String > valueIterator = currentEntry . getValue ( ) . iterator ( ) ; final String firstValue = valueIterator . next ( ) ; qvarMapStrBuilder . append ( ' ' ) . append ( currentEntry . getKey ( ) ) . append ( ' ' ) . append ( " : (data($x" ) . append ( firstValue ) . append ( "/@xml:id)" ) ; //check if there are additional values that we need to constrain if ( valueIterator . hasNext ( ) ) { if ( qvarConstrBuilder . length ( ) > 0 ) { //only add beginning and if it's an additional constraint in the aggregate qvar string qvarConstrBuilder . append ( "\n and " ) ; } while ( valueIterator . hasNext ( ) ) { //process second value onwards final String currentValue = valueIterator . next ( ) ; qvarMapStrBuilder . append ( ",data($x" ) . append ( currentValue ) . append ( "/@xml-id)" ) ; //These constraints specify that the same qvars must refer to the same nodes, //using the XQuery "=" equality //This is equality based on: same text, same node names, and same children nodes qvarConstrBuilder . append ( "$x" ) . append ( firstValue ) . append ( " = $x" ) . append ( currentValue ) ; if ( valueIterator . hasNext ( ) ) { qvarConstrBuilder . append ( " and " ) ; } } } qvarMapStrBuilder . append ( ' ' ) ; if ( entryIterator . hasNext ( ) ) { qvarMapStrBuilder . append ( ' ' ) ; } } qvarMapStrBuilder . append ( "}\n};" ) ; } qvarMapVariable = qvarMapStrBuilder . toString ( ) ; qvarConstraint = qvarConstrBuilder . toString ( ) ; }
Uses the qvar map to generate a XQuery string containing qvar constraints and the qvar map variable which maps qvar names to their respective formula ID s in the result .