}
// process attributes
final AttributesImpl attrs = new AttributesImpl();
if(attributes != null) {
AttributeConstructor constructor;
Sequence attrValues;
QName attrQName;
// first, search for xmlns attributes and declare in-scope namespaces
for (int i = 0; i < attributes.length; i++) {
constructor = attributes[i];
if(constructor.isNamespaceDeclaration()) {
final int p = constructor.getQName().indexOf(':');
if(p == Constants.STRING_NOT_FOUND)
{context.declareInScopeNamespace("", constructor.getLiteralValue());}
else {
final String prefix = constructor.getQName().substring(p + 1);
context.declareInScopeNamespace(prefix, constructor.getLiteralValue());
}
}
}
String v = null;
// process the remaining attributes
for (int i = 0; i < attributes.length; i++) {
context.proceed(this, builder);
constructor = attributes[i];
attrValues = constructor.eval(contextSequence, contextItem);
attrQName = QName.parse(context, constructor.getQName(), "");
final String namespaceURI = attrQName.getNamespaceURI();
if (namespaceURI != null && !namespaceURI.isEmpty() && attrQName.getPrefix() == null) {
String prefix = context.getPrefixForURI(namespaceURI);
if (prefix != null) {
attrQName.setPrefix(prefix);
} else {
//generate prefix
for (final int n = 1; i < 100; i++) {
prefix = "eXnsp"+n;
if (context.getURIForPrefix(prefix) == null) {
attrQName.setPrefix(prefix);
break;
}
prefix = null;
}
if (prefix == null)
{throw new XPathException(this, "Prefix can't be generate.");}
}
}
if (attrs.getIndex(attrQName.getNamespaceURI(), attrQName.getLocalName()) != -1)
{throw new XPathException(this, ErrorCodes.XQST0040, "'" + attrQName.getLocalName() + "' is a duplicate attribute name");}
v = DynamicAttributeConstructor.normalize(this, attrQName, attrValues.getStringValue());
attrs.addAttribute(attrQName.getNamespaceURI(), attrQName.getLocalName(),
attrQName.getStringValue(), "CDATA", v);
}
}
context.proceed(this, builder);
// create the element
final Sequence qnameSeq = qnameExpr.eval(contextSequence, contextItem);
if(!qnameSeq.hasOne())
{throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: the node name should evaluate to a single item");}
final Item qnitem = qnameSeq.itemAt(0);
QName qn;
if (qnitem instanceof QNameValue) {
qn = ((QNameValue)qnitem).getQName();
} else {
//Do we have the same result than Atomize there ? -pb