javaapachexsdqnames

Local part cannot be “null” when creating a QName


I use Apache XmlSchema 2.2.1 to parse XSD schema. I has the following schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.example.com/aigu"
       xmlns="http://www.example.com/aigu"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0">
    <xs:attribute name="label" type="xs:string" />
    <xs:element name="object">
        <xs:complexType>
            <xs:attribute ref="label" form="unqualified"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

The following code produces exception

import org.apache.ws.commons.schema.XmlSchemaCollection;
import org.xml.sax.InputSource;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

public class Aigu {
    public static void main(String[] args) {
        String schema = "HERE_IS_CONTENT_OF_SCHEMA";
        XmlSchemaCollection collection = new XmlSchemaCollection();
        collection.read(new InputSource(new ByteArrayInputStream(schema.getBytes(StandardCharsets.UTF_8))));
    }
}

Stacktrace:

Exception in thread "main" java.lang.IllegalArgumentException: local part cannot be "null" when creating a QName
    at javax.xml.namespace.QName.<init>(QName.java:244)
    at javax.xml.namespace.QName.<init>(QName.java:188)
    at org.apache.ws.commons.schema.utils.XmlSchemaNamedWithFormImpl.setName(XmlSchemaNamedWithFormImpl.java:117)
    at org.apache.ws.commons.schema.utils.XmlSchemaNamedWithFormImpl.setForm(XmlSchemaNamedWithFormImpl.java:105)
    at org.apache.ws.commons.schema.XmlSchemaAttribute.setForm(XmlSchemaAttribute.java:170)
    at org.apache.ws.commons.schema.SchemaBuilder.handleAttribute(SchemaBuilder.java:959)
    at org.apache.ws.commons.schema.SchemaBuilder.handleAttribute(SchemaBuilder.java:923)
    at org.apache.ws.commons.schema.SchemaBuilder.handleComplexType(SchemaBuilder.java:307)
    at org.apache.ws.commons.schema.SchemaBuilder.handleElement(SchemaBuilder.java:420)
    at org.apache.ws.commons.schema.SchemaBuilder.handleSchemaElementChild(SchemaBuilder.java:1512)
    at org.apache.ws.commons.schema.SchemaBuilder.handleXmlSchemaElement(SchemaBuilder.java:659)
    at org.apache.ws.commons.schema.SchemaBuilder.build(SchemaBuilder.java:157)
    at org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:508)
    at org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:717)
    at org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:565)
    at com.netcracker.mediation.transition.model.xmltojava.Aigu.main(Aigu.java:23)

Is it a bug in Apache code or my schema is invalid?


Solution

  • The attribute form cannot be used in an attribute use that has a ref (as constrained in point 3.2 in this paragraph of the XML Schema specification).

    Also, since the target of the reference is a top-level attribute declaration in a schema with a target namespace, its form, if it were allowed to put it explicitly, would have to be qualified.

    It may explain the error, as the trace seems to indicate that it happens there.

    This would be the corrected schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema targetNamespace="http://www.example.com/aigu"
        xmlns="http://www.example.com/aigu"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0">
        <xs:attribute name="label" type="xs:string" />
        <xs:element name="object">
            <xs:complexType>
                <xs:attribute ref="label"/>
            </xs:complexType>
        </xs:element>
    </xs:schema>