Missing namespace for attribute defaults from SAXParser

Patrik Stellmann <[email protected]> Fri, 09 Dec 2016 10:09:46 +0100
Newsgroups gmane.text.xml.xerces-j.user
Message-ID <[email protected]>
Hi,

I'm using org.apache.xerces.parsers.SAXParser and when parsing a file 
with assigned XSD the added default attributes are missing the namespace 
prefix.

The attribute that I have set explicitly is serialized correctly. Ths, 
I'm pretty sure it is a parser issue.

I've added a minimal sample below.

Is this a bug or is there any additional feature I have to activate?

Thanks and regards,
Patrik

------------------------------------
java-code:

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class ParserTest {

    public static void main (String[] args) {
        try {
            final File inputFile     = new File("input.xml");
            final File outputFile     = new File("output.xml");

            final TransformerFactory tf = 
TransformerFactory.newInstance();
            final SAXTransformerFactory stf = (SAXTransformerFactory) tf;

            final TransformerHandler serializer = 
stf.newTransformerHandler();

            XMLReader xmlSource = new SAXParser();
            
xmlSource.setFeature("http://apache.org/xml/features/validation/schema", 
true);

            Result out = new StreamResult(new 
FileOutputStream(outputFile));
            serializer.setResult(out);
            xmlSource.setContentHandler(serializer);
            xmlSource.parse(new 
InputSource(inputFile.toURI().toString()));
            System.out.println("Done!");
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

------------------------------------
input.xml:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="schema.xsd" 
xmlns:ns="http://www.example.org/namespace" ns:attr1="test1"/>

------------------------------------
schema.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:ns="http://www.example.org/namespace" 
elementFormDefault="qualified">
     <xs:import namespace="http://www.example.org/namespace" 
schemaLocation="namespace.xsd"/>
     <xs:element name="root">
         <xs:complexType>
             <xs:attribute ref="ns:attr1" default="default1"/>
             <xs:attribute ref="ns:attr2" default="default2"/>
         </xs:complexType>
     </xs:element>
</xs:schema>

------------------------------------
namespace.xsd:
<xs:schema targetNamespace="http://www.example.org/namespace" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xs:attribute name="attr1"/>
     <xs:attribute name="attr2"/>
</xs:schema>

------------------------------------
generated output.xml:
<root xsi:noNamespaceSchemaLocation="schema.xsd" ns:attr1="test1" 
attr2="default2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:ns="http://www.example.org/namespace"/>

(note the missing "ns:" before attr2)