Hi Siarhei,

Your XML instance document is invalid. The "graph" element in your document
has the namespace "urn:graph", however if you look at the element
declaration in your schema it has no namespace (because it was locally
declared and elementFormDefault="unqualified"). An element with a namespace
and an element without one are quite different. A namespace is part of the
element's name, just like someone's surname is part of their name. It needs
to match the one expected for the element declaration in your schema which
in this case is no namespace at all.

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [EMAIL PROTECTED]
E-mail: [EMAIL PROTECTED]

Siarhei Bushyk <[EMAIL PROTECTED]> wrote on 11/20/2008 09:59:47 AM:

> Hello, all.
> Here is my main class, graph.xsd and graph.xml (in the corresponding
order).
> And the problem:
>
> org.xml.sax.SAXParseException:
> cvc-complex-type.2.4.a: Invalid content was found starting with
> element 'graph'. One of '{graph}' is expected.
>
> Though I have {graph} element with a viewRef id in the root element
> definition.
> Could you please clarify, whether this situation an issue with a
> substitution пroup, or just my fault?
>
>
> import javax.xml.parsers.DocumentBuilder;
> import javax.xml.parsers.DocumentBuilderFactory;
>
> import org.xml.sax.ErrorHandler;
> import org.xml.sax.SAXException;
> import org.xml.sax.SAXParseException;
>
> /**
>  * Configures xml builder facilities to recognize graph.xsd schema
>  * and loades graph.xml to parse it
>  */
> public class Main2 {
>
>     public static void main(String[] args) throws Exception {
>       System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
>             "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
>       DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
>       factory.setAttribute("http://java.sun.
> com/xml/jaxp/properties/schemaLanguage",
>             "http://www.w3.org/2001/XMLSchema";);
>       factory
>             .setAttribute("http://apache.
> org/xml/properties/schema/external-schemaLocation",
>                   "urn:graph /graph.xsd");
>       factory.setAttribute("http://java.sun.
> com/xml/jaxp/properties/schemaLanguage",
>             "http://www.w3.org/2001/XMLSchema";);
>       factory.setFeature("http://xml.org/sax/features/validation";, true);
>
factory.setFeature("http://apache.org/xml/features/validation/schema";,
> true);
>       factory.setFeature("http://apache.org/xml/features/honour-all-
> schemaLocations", true);
>       factory.setNamespaceAware(true);
>       DocumentBuilder builder = factory.newDocumentBuilder();
>       builder.setErrorHandler(new ErrorHandler() {
>           public void error(SAXParseException exception) throws
SAXException {
>             throw exception;
>           }
>           public void fatalError(SAXParseException exception) throws
> SAXException {
>             throw exception;
>           }
>           public void warning(SAXParseException exception) throws
> SAXException {
>             throw exception;
>           }
>       });
>       builder.parse(Main2.class.getResourceAsStream("graph.xml"));
>     }
> }
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="http:
> //www.w3.org/2001/XMLSchema-instance"
>       xmlns:graph="urn:graph" targetNamespace="urn:graph"
>       attributeFormDefault="unqualified"
elementFormDefault="unqualified">
>
>       <complexType name="Appearance">
>             <sequence minOccurs="0" maxOccurs="unbounded">
>                   <element name="icon" minOccurs="0"
maxOccurs="unbounded">
>                         <complexType>
>                               <attribute name="name" type="string" />
>                               <attribute name="url" type="anyURI" />
>                         </complexType>
>                   </element>
>                   <element name="property" minOccurs="0"
maxOccurs="unbounded"
> >
>                         <complexType>
>                               <attribute name="name" type="string" />
>                               <attribute name="value" type="string" />
>                         </complexType>
>                   </element>
>             </sequence>
>             <attribute name="name" />
>       </complexType>
>
>       <complexType name="AbstractGraphCellView" abstract="true">
>             <sequence>
>                   <element name="graph">
>                         <complexType>
>                               <choice minOccurs="1" maxOccurs="1">
>                                     <element ref="graph:viewRef" />
>                                     <element ref="graph:graphView" />
>                               </choice>
>                         </complexType>
>                   </element>
>                   <element name="children" type="graph:ViewRefOrView" />
>                   <element name="parent" type="graph:ViewRefOrView"
>                         xsi:nillable="true" />
>                   <any minOccurs="0" maxOccurs="unbounded"
namespace="##other"
>                         processContents="lax" />
>             </sequence>
>             <attribute name="id" type="string" use="required" />
>             <anyAttribute namespace="##other" processContents="lax" />
>       </complexType>
>
>       <attributeGroup name="BoundsAttributeGroup">
>             <attribute name="bounds" default="0,0,0,0">
>                   <simpleType>
>                         <restriction base="string">
>                               <annotation>
>                                     <documentation>x, y, width, height</
> documentation>
>                               </annotation>
>                               <pattern value="\d*,\d*,\d*,\d*" />
>                         </restriction>
>                   </simpleType>
>             </attribute>
>       </attributeGroup>
>
>       <complexType name="GraphNodeView">
>             <complexContent>
>                   <extension base="graph:AbstractGraphCellView">
>                         <attributeGroup ref="graph:BoundsAttributeGroup"
/>
>                   </extension>
>             </complexContent>
>       </complexType>
>
>       <complexType name="EdgeView">
>             <complexContent>
>                   <extension base="graph:AbstractGraphCellView">
>                         <attribute name="start">
>                               <simpleType>
>                                     <restriction base="string">
>                                           <annotation>
>                                                 <documentation>startX,
startY
> </documentation>
>                                           </annotation>
>                                           <pattern value="\d*,\d*" />
>                                     </restriction>
>                               </simpleType>
>                         </attribute>
>                         <attribute name="end">
>                               <simpleType>
>                                     <restriction base="string">
>                                           <annotation>
>                                                 <documentation>endX,
endY</
> documentation>
>                                           </annotation>
>                                           <pattern value="\d*,\d*" />
>                                     </restriction>
>                               </simpleType>
>                         </attribute>
>                   </extension>
>             </complexContent>
>       </complexType>
>
>       <complexType name="GraphView">
>             <complexContent>
>                   <extension base="graph:AbstractGraphCellView">
>                         <sequence>
>                               <element name="appearance" type=
> "graph:Appearance" />
>                         </sequence>
>                         <attributeGroup ref="graph:BoundsAttributeGroup"
/>
>                   </extension>
>             </complexContent>
>       </complexType>
>
>       <complexType name="GraphCell">
>             <sequence>
>                   <element name="userValue" minOccurs="0">
>                         <complexType>
>                               <sequence>
>                                     <!--
>                                           The XML processor attempts
> to obtain the schema for the required
>                                           namespaces and validate
> any element from those namespaces;
>                                           however, if the schema
> cannot be obtained, no errors will occur.
>                                     -->
>                                     <any namespace="##any" minOccurs="0"
> processContents="skip" />
>                               </sequence>
>                         </complexType>
>                   </element>
>                   <any minOccurs="0" maxOccurs="unbounded"
namespace="##other"
>                         processContents="lax" />
>             </sequence>
>             <attribute name="id" type="string" />
>             <anyAttribute namespace="##other" processContents="lax" />
>       </complexType>
>
>       <complexType name="GraphNode">
>             <complexContent>
>                   <extension base="graph:GraphCell">
>                         <sequence>
>                               <element name="allowsChildren"
type="boolean"
> default="false" />
>                               <element name="parent" type="graph:
> CellRefOrCell" />
>                               <element name="children">
>                                     <complexType>
>                                           <sequence minOccurs="0"
maxOccurs=
> "unbounded">
>                                                 <group ref="graph:
> CellRefOrCellGroup" />
>                                           </sequence>
>                                     </complexType>
>                               </element>
>                         </sequence>
>                   </extension>
>             </complexContent>
>       </complexType>
>
>       <complexType name="Edge">
>             <complexContent>
>                   <extension base="graph:GraphCell">
>                         <sequence>
>                               <element name="source" type="graph:
> CellRefOrCell" />
>                               <element name="target" type="graph:
> CellRefOrCell" />
>                         </sequence>
>                   </extension>
>             </complexContent>
>       </complexType>
>
>       <complexType name="GraphDataModel">
>             <sequence>
>                   <element name="roots">
>                         <complexType>
>                               <sequence minOccurs="0"
maxOccurs="unbounded">
>                                     <group ref="graph:CellRefOrCellGroup"
/>
>                               </sequence>
>                         </complexType>
>                         <!-- TODO: key definition -->
>                         <key name="GraphCellKey">
>                               <selector xpath=". | .//cellRef" />
>                               <field xpath="@id" />
>                         </key>
>                   </element>
>             </sequence>
>       </complexType>
>
>       <complexType name="Ref">
>             <attribute name="id" type="string" />
>       </complexType>
>
>       <group name="CellRefOrCellGroup">
>             <choice>
>                   <element ref="graph:cellRef" />
>                   <element ref="graph:cell" />
>             </choice>
>       </group>
>
>       <complexType name="CellRefOrCell">
>             <group ref="graph:CellRefOrCellGroup" />
>       </complexType>
>
>       <group name="ViewRefOrViewGroup">
>             <choice>
>                   <element ref="graph:viewRef" />
>                   <element ref="graph:view" />
>                   <element ref="graph:graphNodeView" />
>                   <element ref="graph:edgeView" />
>                   <element ref="graph:graphView" />
>             </choice>
>       </group>
>
>       <complexType name="ViewRefOrView">
>             <group ref="graph:ViewRefOrViewGroup" />
>       </complexType>
>
>       <element name="cell" type="graph:GraphCell" />
>       <element name="cellRef" type="graph:Ref" />
>
>       <element name="view" type="graph:AbstractGraphCellView" />
>       <element name="graphNodeView" type="graph:GraphNodeView" />
>       <element name="edgeView" type="graph:EdgeView" />
>       <element name="graphView" type="graph:GraphView" />
>       <element name="viewRef" type="graph:Ref" />
> </schema>
>
>
> <?xml version="1.0"?>
> <view id="d" xmlns="urn:graph" xmlns:xsi="http://www.w3.
> org/2001/XMLSchema-instance"
>       xsi:type="GraphView" bounds="1,1,2,3">
>
>       <graph>
>             <viewRef id="d"/>
>       </graph>
>       <!--
>             Other elements?
>       -->
> </view>

Reply via email to