You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by bu...@apache.org on 2004/08/23 14:32:39 UTC

DO NOT REPLY [Bug 30802] New: - Getting org.xml.sax.SAXParseException: cvc-complex-type.3.1 while parsing XML with elements having same name but different complex types

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=30802>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=30802

Getting org.xml.sax.SAXParseException: cvc-complex-type.3.1 while parsing XML with elements having same name but different complex types

           Summary: Getting org.xml.sax.SAXParseException: cvc-complex-
                    type.3.1 while parsing XML with elements having same
                    name but different complex types
           Product: Xerces-J
           Version: 1.4.4
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: SAX
        AssignedTo: xerces-j-dev@xml.apache.org
        ReportedBy: jbehal@sapient.com


I created an XSD which contains elements with same name but different complex 
types. For example, the high level XSD looks like -

<xs:element name = "Field" type = "ComplexType1">
<xs:element name = "Field" type = "ComplexType2" minOccurs=0>
<xs:element name = "Field" type = "ComplexType3">

- while the corresponding XML would be -

<Field type="Field1">
<Field type="Field2">
<Field type="Field3">

If I validate this XML against the XSD it works fine.
But if I remove the second element from the XML as shown below as it's 
minOccurs = 0

<Field type="Field1">
<Field type="Field3">

and try to validate it against the XSD it throws an exception as shown below

org.xml.sax.SAXParseException: cvc-complex-type.3.1: Value 'Field3' of attribute
 'name' of element 'Field' is not valid with respect to the corresponding attrib
ute use. Attribute 'name' has a fixed value of 'Field2'.

But this works fine with XML SPY without any problems.

Could you please let me know why it's happening and is it a bug in Xerces?
If it's a bug then is there a work around for this?

I am pasting the Actual XSD and XML below for your reference

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:mm2="mm2" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="mm2" attributeFormDefault="unqualified" 
elementFormDefault="qualified">
	<xs:element name="Test_element" type="mm2:Test_Type">
	</xs:element>
	
	<xs:simpleType name="SHORT_TEXT255_Type">
		<xs:restriction base="xs:string">
			<xs:maxLength value="255"/>
		</xs:restriction>
	</xs:simpleType>
	
	
	<xs:complexType name="Test_Type">
		<xs:sequence minOccurs="0" maxOccurs="1">
					<xs:choice minOccurs="1" maxOccurs="1">
						<xs:element minOccurs="0" 
name="Field" maxOccurs="1">
							<xs:complexType>
							
	<xs:simpleContent>
								
	<xs:extension base="mm2:SHORT_TEXT255_Type">
									
	<xs:attribute type="xs:string" fixed="Field1" name="name"/>
								
	</xs:extension>
							
	</xs:simpleContent>
							</xs:complexType>
						</xs:element>
					</xs:choice>
					<xs:choice minOccurs="0" maxOccurs="1">
						<xs:element minOccurs="0" 
name="Field" maxOccurs="1">
							<xs:complexType>
							
	<xs:simpleContent>
								
	<xs:extension base="mm2:SHORT_TEXT255_Type">
									
	<xs:attribute type="xs:string" fixed="Field2" name="name"/>
								
	</xs:extension>
							
	</xs:simpleContent>
							</xs:complexType>
						</xs:element>
					</xs:choice>
					<xs:choice minOccurs="1" maxOccurs="1">
						<xs:element minOccurs="1" 
name="Field" maxOccurs="1">
							<xs:complexType>
							
	<xs:simpleContent>
								
	<xs:extension base="mm2:SHORT_TEXT255_Type">
									
	<xs:attribute type="xs:string" fixed="Field3" name="name"/>
								
	</xs:extension>
							
	</xs:simpleContent>
							</xs:complexType>
						</xs:element>
					</xs:choice>
				</xs:sequence>
				<xs:attribute type="xs:string" fixed="Test" 
name="type"/>
	</xs:complexType>
	</xs:schema>


XML:

<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSPY v5 U (http://www.xmlspy.com)-->
<Test_element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="mm2">
	<Field name="Field1">String</Field>
	<Field name="Field3">String</Field>
</Test_element>


Exception thrown by Parser (thrown for both SAX and DOM parsing):

org.xml.sax.SAXParseException: cvc-complex-type.3.1: Value 'Field3' of attribute
 'name' of element 'Field' is not valid with respect to the corresponding attrib
ute use. Attribute 'name' has a fixed value of 'Field2'.


And here is the code which I am using for parsing


        /** <p>XML representation of boolean "true".</p>*/
        private static final String YES = "yes";

        /** <p>XML representation of boolean "false".</p>*/
        private static final String NO = "no";

        /** <p>Character encoding used when reading files.</p>*/
        private static final String CHAR_ENCODING = "UTF8";

        /** <p>Sets the namespace aware property for parsers</p>*/
        private static final boolean NAME_SPACE_AWARE = true;

        /** <p>Sets the validating property for parsers</p>*/
        private static final boolean VALIDATING = true;

        /** <p>Defines schema language to be used by parser for 
validation.</p>*/
        private static final String SCHEMA_LANGUAGE =
                                            "http://java.sun.com/xml/jaxp/proper
ties/schemaLanguage";
        /**
         * <p>The URI value of schema language specification property. This 
value supports W3C XML
         * Schema specification to be compliant with JAXP 1.2 specification.</p>
         */
        private static final String SCHEMA_LANGUAGE_VAL 
= "http://www.w3.org/2001/XMLSchema";

    private static final String SCHEMA_SOURCE =
                                               "http://java.sun.com/xml/jaxp/pro
perties/schemaSource";

   public static void SaxParser(String[] args) {
			try {
				Reader xmlReader;
				Reader xsdReader;

				xmlReader = new FileReader
("E:\\apps\\parsertest\\Untitled2.xml");
				xsdReader = new FileReader
("E:\\apps\\parsertest\\Untitled2.xsd");

				SAXParserFactory factory = 
SAXParserFactory.newInstance();

				// Configure SAXParserFactory to provide 
parsers that are namespace aware.
				factory.setNamespaceAware(NAME_SPACE_AWARE);
				// Configure SAXParserFactory to provide 
parsers that are validating. This property
				// must have the value true for any of the 
property strings defined below to take
				// effect.
				factory.setValidating(VALIDATING);

				SAXParser parser = factory.newSAXParser();

				// Setting the schema language for xml schema 
validation
				parser.setProperty(SCHEMA_LANGUAGE, 
SCHEMA_LANGUAGE_VAL);
				// Setting the schema source for xml schema 
validation
				parser.setProperty(SCHEMA_SOURCE, new 
InputSource(xsdReader));

			    DefaultHandler handler = new XmlDefaultHandler();
			    parser.parse(new InputSource(xmlReader), handler);
			}
			catch (FactoryConfigurationError e) {
			    System.out.println(e.toString());
			}
			catch (ParserConfigurationException e) {
			    System.out.println(e.toString());
			}
			catch (SAXException e) {
			    System.out.println(e.toString());
			}
			catch (IOException e) {
			    System.out.println(e.toString());
			}
}

public static void DomParser() {
			try {
				Reader xmlReader;
				Reader xsdReader;

				xmlReader = new FileReader
("E:\\apps\\parsertest\\validrequest-create-approveedit.xml");
				xsdReader = new FileReader
("E:\\apps\\parsertest\\Untitled1.xsd");

				DocumentBuilderFactory factory =
				    DocumentBuilderFactory.newInstance();

				// Configure SAXParserFactory to provide 
parsers that are namespace aware.
				factory.setNamespaceAware(NAME_SPACE_AWARE);
				// Configure SAXParserFactory to provide 
parsers that are validating. This property
				// must have the value true for any of the 
property strings defined below to take
				// effect.
				factory.setValidating(VALIDATING);
				// Setting the schema language for xml schema 
validation
				factory.setAttribute(SCHEMA_LANGUAGE, 
SCHEMA_LANGUAGE_VAL);
				// Setting the schema source for xml schema 
validation
				factory.setAttribute(SCHEMA_SOURCE, new 
InputSource(xsdReader));

				DocumentBuilder builder = 
factory.newDocumentBuilder();



			    //DefaultHandler handler = new XmlDefaultHandler();
			    builder.parse(new InputSource(xmlReader));
			}
			catch (FactoryConfigurationError e) {
			    System.out.println(e.toString());
			}
			catch (ParserConfigurationException e) {
			    System.out.println(e.toString());
			}
			catch (SAXException e) {
			    System.out.println(e.toString());
			}
			catch (IOException e) {
			    System.out.println(e.toString());
			}
		}
    /**
     * <p>Provides <code>DefaultHandler</code> implementation to handles 
callbacks during parsing
     * and validation of XML documents. Returns the <code>Exception</code> as 
it is if any failure
     * occurs.</p>
     */
    public static class XmlDefaultHandler extends DefaultHandler {

        /**
         * @see org.xml.sax.ErrorHandler#error(SAXParseException)
         */
        public void error(SAXParseException spe) throws SAXException {
            throw spe;
        }

        /**
         * @see org.xml.sax.ErrorHandler#fatalError(SAXParseException)
         */
        public void fatalError(SAXParseException spe) throws SAXException {
            throw spe;
        }

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org