*
The moose likes EJB and other Java EE Technologies and the fly likes XML schema fails to load Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "XML schema fails to load " Watch "XML schema fails to load " New topic
Author

XML schema fails to load

Aaron Stromas
Greenhorn

Joined: Jan 10, 2012
Posts: 4
Greetings,

I have an XSD file which validates in the W3C schema validator http://www.w3.org/2001/03/webdata/xsv but Schema object fails to instantiate from it.

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File("conf/configuration.xsd"));

The newSchema method generates SAXException

Exception in thread "main" org.xml.sax.SAXParseException: s4s-att-not-allowed: Attribute 'name' cannot appear in element 'element'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:2537)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:2528)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSAttributeChecker.reportSchemaError(XSAttributeChecker.java:1622)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSAttributeChecker.checkAttributes(XSAttributeChecker.java:1140)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSAttributeChecker.checkAttributes(XSAttributeChecker.java:996)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseLocal(XSDElementTraverser.java:151)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseLocalElements(XSDHandler.java:2049)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:582)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:552)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:519)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:485)
at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:210)
at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:594)
at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:610)
at axm.tool.user.test.main(test.java:20)

The XSD document is below. I'm completely baffled. Any suggestions? TIA

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="configuration"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="configuration"
elementFormDefault="qualified">

<xs:element name="configuration">
<xs:complexType>
<xs:sequence>
<xs:element name="property" ref="property" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="property">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="value" ref="scalar" minOccurs="1" maxOccurs="1"/>
<xs:element name="list" ref="list" minOccurs="1" maxOccurs="1"/>
<xs:element name="map" ref="map" minOccurs="1" maxOccurs="1"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

<xs:element name="map">
<xs:complexType>
<xs:sequence>
<xs:element name="entry" type="EntryType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="default" type="EntryType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="list">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="default" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="EntryType">
<xs:sequence>
<xs:element name="key" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>

<xs:element name="scalar">
<xs:complexType>
<xs:sequence>
<xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="default" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Andreas Koenig
Greenhorn

Joined: Jan 10, 2012
Posts: 7

You should either use type declarations like this:

or leave out the name-attribute when using the ref-attribute:
Aaron Stromas
Greenhorn

Joined: Jan 10, 2012
Posts: 4
Thank you, Andreas. I stumbled upon using named type rather that ref solution on my own last night. Given than the W3C verifier did not report any errors, should this be considered as a bug?

-a
 
 
subject: XML schema fails to load
 
Similar Threads
converting xsd to html
How to convert from sql to xml
JAXB XJC compiling Issue
What is "NewDataSet" in XSD file?
Help me for xsd creation