private void declareExternalAndXQJVariables(final XQueryContext context,
final ElementImpl variables) throws XPathException {
final ValueSequence varSeq = new ValueSequence();
variables.selectChildren(new NameTest(Type.ELEMENT, new QName(Variable.xmlKey(), Namespaces.EXIST_NS)), varSeq);
for (final SequenceIterator i = varSeq.iterate(); i.hasNext();) {
final ElementImpl variable = (ElementImpl) i.nextItem();
// get the QName of the variable
final ElementImpl qname = (ElementImpl) variable.getFirstChild(new NameTest(Type.ELEMENT, new QName("qname", Namespaces.EXIST_NS)));
String localname = null, prefix = null, uri = null;
NodeImpl child = (NodeImpl) qname.getFirstChild();
while (child != null) {
if ("localname".equals(child.getLocalName())) {
localname = child.getStringValue();
} else if ("namespace".equals(child.getLocalName())) {
uri = child.getStringValue();
} else if ("prefix".equals(child.getLocalName())) {
prefix = child.getStringValue();
}
child = (NodeImpl) child.getNextSibling();
}
if (uri != null && prefix != null) {
context.declareNamespace(prefix, uri);
}
if (localname == null) {
continue;
}
final QName q;
if (prefix != null && localname != null) {
q = new QName(localname, uri, prefix);
} else {
q = new QName(localname, uri, XMLConstants.DEFAULT_NS_PREFIX);
}
// get serialized sequence
final NodeImpl value = variable.getFirstChild(new NameTest(Type.ELEMENT, Marshaller.ROOT_ELEMENT_QNAME));
final Sequence sequence;
try {
sequence = value == null ? Sequence.EMPTY_SEQUENCE : Marshaller.demarshall(value);
} catch (final XMLStreamException xe) {
throw new XPathException(xe.toString());