Not user friendly, but working as designed. No prefix is defined for 
attribute default values (see schema spec here [1]).

Some of the more recent XML serialization APIs (LSSerializer / 
XMLStreamWriter) are more namespace-aware and have the ability to fix up 
namespaces during serialization.

Thanks.

[1] https://www.w3.org/TR/xmlschema-1/#sic-attrDefault

Michael Glavassevich
XML Technologies and WAS Development
IBM Toronto Lab
E-mail: mrgla...@ca.ibm.com
E-mail: mrgla...@apache.org

Patrik Stellmann <pat...@volleynet.de> wrote on 12/09/2016 04:09:46 AM:
 
> 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)
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscr...@xerces.apache.org
> For additional commands, e-mail: j-users-h...@xerces.apache.org


Reply via email to