Bug #4353


Extra namespaces present when serializing XdmValue using Serializer API

Added by Radu Coravu over 4 years ago. Updated over 4 years ago.

Start date:
Due date:
% Done:


Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:


I have an "net.sf.saxon.s9api.XdmValue" which resulted from an XQuery update operation. I create a serializer "net.sf.saxon.s9api.Processor.newSerializer(Writer)" and then use it to serialize the value:


Problem is that with Saxon 9.9 I get extra not-necessary namespaces serialized, something like:

  <p:url href="" xmlns:p=""/>

so I would prefer somehow to have an extra namespace reducer when writing the content.

I looked at the implementation of "net.sf.saxon.s9api.Processor.writeXdmValue(XdmValue, Destination)" which seems to be adding a namespace reducer on the "else" clause so I used the following workaround on my side:

      processor.writeXdmValue(rootElement, new AbstractDestination() {
        public Receiver getReceiver(PipelineConfiguration pipe, SerializationProperties params)
            throws SaxonApiException {
          return serializer.getReceiver(pipe, params);
        public void close() throws SaxonApiException {

to force the code on the "else" branch and add that extra namespace reducer. But maybe "serializer.serializeXdmValue(rootElement);" could by default somehow use an namespace reducer by default.

Actions #1

Updated by Radu Coravu over 4 years ago

To refine my XML example, the fully serialized XML fragment using the default Serializer approach looks like this:

<p:personnel xmlns:p="">
  <p:person id="harris.anderson" photo="personal-images/harris.anderson.jpg">
    <extra><p:url href="" xmlns:p=""/></extra>

so that "p:url" element has an extra xmlns:p="" declaration which is also on the root element.

Actions #2

Updated by Michael Kay over 4 years ago

It would be interesting to know how the redundant namespace declaration got into the XdmValue in the first place; perhaps the right fix would be to stop that happening.

We've made some efforts to formalize the interface between the "transformation" and "destination" sides of the Receiver interface: see the Javadoc for class RegularSequenceChecker. The rules (in 9.9) don't say they're can't be redundant namespaces. In 10.0 the interface changes so that the startElement() event includes a NamespaceMap object which must include all in-scope namespaces for an element; that puts the onus firmly on the destination side to eliminate duplicates when serializing.

This means that any solution for 9.9 can afford to be a bit pragmatic, and I'm inclined to tackle it by adding a NamespaceReducer to the Receiver returned by QueryResult.serializeSequence(), which underpins Serializer.serailize(XdmValue).

Actions #3

Updated by Michael Kay over 4 years ago

  • Category set to Serialization
  • Status changed from New to Resolved
  • Assignee set to Michael Kay
  • Applies to branch 9.9 added
  • Fix Committed on Branch 9.9 added

I've patched 9.9 to add a NamespaceReducer in QueryResult.serializeSequence(), as suggested. Regression tested only, since we don't have a repro.

Actions #4

Updated by Radu Coravu over 4 years ago

Thanks Michael.

Actions #5

Updated by O'Neil Delpratt over 4 years ago

  • Status changed from Resolved to Closed
  • Fixed in Maintenance Release added

Patch committed to the Saxon maintenance release.

Please register to edit this issue

Also available in: Atom PDF