I'm not entirely sure what's going on here (what are "from" and "to"?) but the essence seems to be that you have an element which is in no namespace, which means by definition it has no prefix, and then you are trying to add a namespace declaration equivalent to xmlns="some-uri"; this declaration would mean an unprefixed element is in namespace "some-uri", but it isn't, it's in the non-namespace.

Michael Kay
Saxonica


On 16/07/2010 20:44, Norman Walsh wrote:
Hi Michael,

In my implementation of namespace renaming, I'm stumbling over

net.sf.saxon.s9api.SaxonApiException: Cannot output a namespace node for the default namespace when the element is in no namespace
	at net.sf.saxon.s9api.XQueryEvaluator.run(XQueryEvaluator.java:305)

Writing a self-contained test case for this is going to be a bit
tricky, so I thought I'd just try a little code and see if the problem
is obvious to you. :-)

As I walk over the source tree, producing a result tree, the code for
a start element looks (roughly) like this:

  public boolean processStartElement(XdmNode node) throws SaxonApiException {
        NodeInfo inode = node.getUnderlyingNode();
        NamePool pool = inode.getNamePool();
        int inscopeNS[] = inode.getDeclaredNamespaces(null);
        int newNS[] = null;
        int nameCode = inode.getNameCode();
        int typeCode = inode.getTypeAnnotation() & NamePool.FP_MASK;

        if (inscopeNS.length > 0) {
            int countNS = 0;

            // Experiment: try throwing the decl away if it's for ""
            for (int pos = 0; pos < inscopeNS.length; pos++) {
                int ns = inscopeNS[pos];
                String uri = pool.getURIFromNamespaceCode(ns);
                if (!from.equals(uri) || !"".equals(to)) {
                    countNS++;
                }
            }

            newNS = new int[countNS];
            int newPos = 0;
            for (int pos = 0; pos < inscopeNS.length; pos++) {
                int ns = inscopeNS[pos];
                String pfx = pool.getPrefixFromNamespaceCode(ns);
                String uri = pool.getURIFromNamespaceCode(ns);
                if (from.equals(uri)) {
                    if ("".equals(to)) {
                        // Nevermind, we're throwing the namespace away
                    } else {
                        int newns = pool.getNamespaceCode(pfx,to);
                        if (newns < 0) {
                            newns = pool.allocateNamespaceCode(pfx,to);
                        }
                        newNS[newPos++] = newns;
                    }
                } else {
                    newNS[newPos++] = ns;
                }
            }
        }

        // Careful, we're messing with the namespace bindings
        // Make sure the nameCode is right...
        String pfx = pool.getPrefix(nameCode);
        String uri = pool.getURI(nameCode);

        if (from.equals(uri)) {
            if ("".equals(to)) {
                pfx = "";
            }

            nameCode = pool.allocate(pfx,to,node.getNodeName().getLocalName());
        }

        matcher.addStartElement(nameCode, typeCode, newNS);

        return true;
    }

Where matcher.addStartElement passes things along to a receiver:

    public void addStartElement(int nameCode, int typeCode, int nscodes[]) {
        int locId;
        String sysId = receiver.getSystemId();
        if (sysId == null) {
            locId = 0;
        } else {
            locId = xLocationProvider.allocateLocation(sysId);
        }

        try {
            receiver.startElement(nameCode, typeCode, locId, 0);
            if (nscodes != null) {
                for (int ns : nscodes) {
                    receiver.namespace(ns, 0);
                }
            }
        } catch (XPathException e) {
            throw new XProcException(e);
        }
    }

AFAICT, I'm constructing a nameCode for a name that's not in a
namespace, so I'm confused by the subsequent report that Saxon cannot
"output a namespace node for the default namespace when the element is
in no namespace"

Do I have to do something special to "undeclare" the default namespace?

                                        Be seeing you,
                                          norm

  
------------------------------------------------------------------------------ This SF.net email is sponsored by Sprint What will you do first with EVO, the first 4G phone? Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________ saxon-help mailing list archived at http://saxon.markmail.org/ saxon-help@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/saxon-help