You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Patrik Stellmann <pa...@volleynet.de> on 2016/12/09 09:09:46 UTC

Missing namespace for attribute defaults from SAXParser

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-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Missing namespace for attribute defaults from SAXParser

Posted by Patrik Stellmann <pa...@volleynet.de>.
Thank for the quick answer, Michael. Will look for a workaround then.

Patrik

Am 09.12.2016 um 14:13 schrieb Michael Glavassevich:
> 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: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org
>
> Patrik Stellmann <pa...@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-unsubscribe@xerces.apache.org
> > For additional commands, e-mail: j-users-help@xerces.apache.org
>


Re: Missing namespace for attribute defaults from SAXParser

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
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: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Patrik Stellmann <pa...@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-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org