Project

Profile

Help

Bug #3068

closed

Cannot output a namespace node for the default namespace when the element is in no namespace

Added by Radu Coravu about 8 years ago. Updated over 7 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
JAXP Java API
Sprint/Milestone:
-
Start date:
2016-12-13
Due date:
% Done:

100%

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

Description

When an XML element is defined in the XML Schema as having a namespaced attribute with a default value, the identity transform breaks when serializing the XML.

XML defAttr.xml

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="main.xsd"/>

XML Schema main.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns:ns1="defAttrNS">

    <xs:import namespace="defAttrNS" schemaLocation="attributes.xsd"/>
    <xs:element name="root">
        <xs:complexType>
            <xs:attribute ref="ns1:defAttr1" default="defVal"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

Java code:

public class RunTransformationWithSAXParser {

	private static ErrorListener errorListener = new ErrorListener() {
		@Override
		public void warning(TransformerException ex) throws TransformerException {
			System.out.println("Warning: " + ex + "\nLocation: " + ex.getLocator());
		}

	@Override
	public void fatalError(TransformerException ex) throws TransformerException {
		System.out.println("Error: " + ex.getMessage());
	}

		@Override
		public void error(TransformerException ex) throws TransformerException {
			System.out.println("Error: " + ex + "\nLocation: " + ex.getLocator());
		}
	};

	private static String xmlPath = "samples/dita_xsd_based/defAttr.xml";
	private static String outPath = "samples/dita_xsd_based/out.xml";

	public static void main(String[] args) throws TransformerException, InterruptedException, SAXNotRecognizedException, SAXNotSupportedException {
		// Create transformer
		TransformerFactoryImpl transformerFactory = new EnterpriseTransformerFactory();
		transformerFactory.setErrorListener(errorListener);
		
		transformerFactory.getConfiguration().setConfigurationProperty(
				FeatureKeys.SCHEMA_VALIDATION_MODE,
				Validation.toString(Validation.STRICT));
		
		transformerFactory.setAttribute(net.sf.saxon.lib.FeatureKeys.XSLT_SCHEMA_AWARE, true);
		
		Transformer transformer = transformerFactory.newTransformer();
		transformer.setErrorListener(errorListener);

		// Create XML SAX source
		SAXSource xmlSource = new SAXSource(new InputSource(new File(xmlPath).toURI().toString()));
		SAXParser saxParser = new SAXParser();
		
	    //EXM-11081 Set this feature so that the default attributes from the schema are reported...
	    saxParser.setFeature("http://apache.org/xml/features/validation/schema", true);
		
		xmlSource.setXMLReader(saxParser);

		// Output stream result
		File outFile = new File(outPath);
		if (outFile.exists()) {
			outFile.delete();
		}
		Result outputTarget = new StreamResult(outFile);

		// Run transformation
		System.out.println("Run transformation...");
		try {
			transformer.transform(xmlSource, outputTarget);
			System.out.println("Transformation done.");
		} catch (Exception e) {
			System.out.println("Transformation failed: " + e);
			e.printStackTrace();
		}
	}
}

This used to work with Saxon prior to 9.7. Looking at this answer to a similar question on the Xerces mailing list:

http://mail-archives.apache.org/mod_mbox/xerces-j-users/201612.mbox/browser

it would seem that Xerces is not required to provide a qname for the default attribute on the SAX calls, it only provides local name and namespace.

Please register to edit this issue

Also available in: Atom PDF