Topic
  • 4 replies
  • Latest Post - ‏2011-03-01T12:13:47Z by Lul4n
Lul4n
Lul4n
3 Posts

Pinned topic problem while validating xml using sax+xsd

‏2011-02-28T19:50:44Z |
Hey Guys

i ran into a problem parsing xml files using the sax parser and a custom xsd.
I gues its some namespace trouble but i dont get it by myself ...
Alway get the following error msg while validating:
Error planetclasses.xml:5:2: cvc-elt.1: Cannot find the declaration of element 'planetclasses'.

i hope one of you might have a hint

greez julian

thats my java code:

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); Schema schema = schemaFactory.newSchema(schemaFile); Validator validator = schema.newValidator(); docBuilderFactory.setSchema(schema); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(xmlFile); validator.validate(

new DOMSource(doc));

here goes my xsd file:

<?xml version=
"1.0" encoding=
"UTF-8"?> <xsd:schema xmlns:xsd=
"http://www.w3.org/2001/XMLSchema" targetNamespace=
"http://www.shangri-la-online.de" xmlns=
"http://www.shangri-la-online.de" elementFormDefault=
"qualified" attributeFormDefault=
"unqualified" > <xsd:element name=
"planetclasses" > <xsd:complexType> <xsd:sequence> <xsd:element name=
"planetclass" type=
"planetclassType" /> </xsd:sequence> </xsd:complexType> </xsd:element>   <xsd:complexType name=
"ingredientType" > <xsd:simpleContent> <xsd:extension base=
"xsd:string"> <xsd:attribute name=
"magnitude" type=
"xsd:positiveInteger" use=
"required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name=
"temperatureType" > <xsd:simpleContent> <xsd:extension base=
"xsd:integer"> <xsd:attribute name=
"unit" type=
"xsd:string" use=
"required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name=
"diameterType" > <xsd:simpleContent> <xsd:extension base=
"xsd:integer"> <xsd:attribute name=
"unit" type=
"xsd:string" use=
"required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name=
"gravitationType" > <xsd:simpleContent> <xsd:extension base=
"xsd:decimal"> <xsd:attribute name=
"unit" type=
"xsd:string" use=
"required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name=
"planetclassType" mixed=
"true" > <xsd:sequence> <xsd:element name=
"description" type=
"xsd:string" minOccurs=
"0" maxOccurs=
"1" /> <xsd:element name=
"surface" minOccurs=
"1" maxOccurs=
"1" > <xsd:complexType> <xsd:sequence> <xsd:element name=
"ingredient" type=
"ingredientType" minOccurs=
"1" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=
"atmosphere" minOccurs=
"1" maxOccurs=
"1" > <xsd:complexType> <xsd:sequence> <xsd:element name=
"ingredient" type=
"ingredientType" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=
"temperatures" minOccurs=
"1" maxOccurs=
"1" > <xsd:complexType mixed=
"true"> <xsd:sequence> <xsd:element name=
"minimum" type=
"temperatureType" minOccurs=
"1" maxOccurs=
"1" /> <xsd:element name=
"maximum" type=
"temperatureType" minOccurs=
"1" maxOccurs=
"1" /> <xsd:element name=
"average" type=
"temperatureType" minOccurs=
"1" maxOccurs=
"1" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=
"diameter" minOccurs=
"1" maxOccurs=
"1" type=
"diameterType" /> <xsd:element name=
"gravitation" minOccurs=
"1" maxOccurs=
"1" type=
"gravitationType" /> </xsd:sequence> <xsd:attribute name=
"title" type=
"xsd:string" /> </xsd:complexType> </xsd:schema>

and finally my example xml file:

<?xml version=
"1.0" encoding=
"UTF-8"?> <planetclasses xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance" xmlns=
"http://www.shangri-la-online.de" > <planetclass title=
"A-1"> <description> Earth alike planet with a stable atmosphere that allows human life. </description> <surface> <ingredient magnitude=
"55">metal</ingredient> <ingredient magnitude=
"15">silicium</ingredient> <ingredient magnitude=
"30">oxygen</ingredient> </surface> <atmosphere> <ingredient magnitude=
"20">oxygen</ingredient> <ingredient magnitude=
"80">nitrogen</ingredient> </atmosphere> <temperatures> <minimum unit=
"°C">-50</minimum> <maximum unit=
"°C">50</maximum> <average unit=
"°C">20</average> </temperatures> <diameter unit=
"km">13000</diameter> <gravitation unit=
"m/s²">9.81</gravitation> </planetclass> </planetclasses>
Updated on 2011-03-01T12:13:47Z at 2011-03-01T12:13:47Z by Lul4n
  • SystemAdmin
    SystemAdmin
    1830 Posts

    Re: problem while validating xml using sax+xsd

    ‏2011-03-01T07:25:01Z  
    [0] The default value of cardinality facet is 1 for both minOccurs and maxOccurs. In case you want to make it explicit for reminding, it is fine, but it is not mandatory.

    [1] However, any time it should be different from the default (1), you need to make it explicit. The problem is that surface and atmosphere should allow more than one ingredient. That you should make it explicit.
    
    <xsd:element name=
    "surface" minOccurs=
    "1" maxOccurs=
    "1" > <xsd:complexType> <xsd:sequence> <xsd:element name=
    "ingredient" type=
    "ingredientType" minOccurs=
    "1" maxOccurs=
    "unbounded" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=
    "atmosphere" minOccurs=
    "1" maxOccurs=
    "1" > <xsd:complexType> <xsd:sequence> <xsd:element name=
    "ingredient" type=
    "ingredientType" maxOccurs=
    "unbounded" /> </xsd:sequence> </xsd:complexType> </xsd:element>
    
  • Lul4n
    Lul4n
    3 Posts

    Re: problem while validating xml using sax+xsd

    ‏2011-03-01T09:52:01Z  
    [0] The default value of cardinality facet is 1 for both minOccurs and maxOccurs. In case you want to make it explicit for reminding, it is fine, but it is not mandatory.

    [1] However, any time it should be different from the default (1), you need to make it explicit. The problem is that surface and atmosphere should allow more than one ingredient. That you should make it explicit.
    <pre class="jive-pre"> <xsd:element name= "surface" minOccurs= "1" maxOccurs= "1" > <xsd:complexType> <xsd:sequence> <xsd:element name= "ingredient" type= "ingredientType" minOccurs= "1" maxOccurs= "unbounded" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name= "atmosphere" minOccurs= "1" maxOccurs= "1" > <xsd:complexType> <xsd:sequence> <xsd:element name= "ingredient" type= "ingredientType" maxOccurs= "unbounded" /> </xsd:sequence> </xsd:complexType> </xsd:element> </pre>
    thanks a lot, i really missed to define all quantifiers...

    well if i remove the namespace its validates correct... but if the namespace remains, it still throws the same error:
    
    [Error] planetclasses.xml:5:2: cvc-elt.1: Cannot find the declaration of element 
    'planetclasses'.
    


    any ideas?

    greez julian

    updated xsd:
    
    <?xml version=
    "1.0" encoding=
    "UTF-8"?> <xsd:schema xmlns:xsd=
    "http://www.w3.org/2001/XMLSchema" targetNamespace=
    "http://www.shangri-la-online.de/schema/planet" xmlns=
    "http://www.shangri-la-online.de/schema/planet" elementFormDefault=
    "qualified" attributeFormDefault=
    "unqualified" > <xsd:complexType name=
    "ingredientType" > <xsd:simpleContent> <xsd:extension base=
    "xsd:string"> <xsd:attribute name=
    "magnitude" type=
    "xsd:positiveInteger" use=
    "required" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name=
    "temperatureType" > <xsd:simpleContent> <xsd:extension base=
    "xsd:integer"> <xsd:attribute name=
    "unit" type=
    "xsd:string" use=
    "required" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name=
    "diameterType" > <xsd:simpleContent> <xsd:extension base=
    "xsd:integer"> <xsd:attribute name=
    "unit" type=
    "xsd:string" use=
    "required" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name=
    "gravitationType" > <xsd:simpleContent> <xsd:extension base=
    "xsd:decimal"> <xsd:attribute name=
    "unit" type=
    "xsd:string" use=
    "required" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name=
    "planetclassType" > <xsd:all> <xsd:element name=
    "description" type=
    "xsd:string" minOccurs=
    "0" maxOccurs=
    "1" /> <xsd:element name=
    "surface" minOccurs=
    "1" maxOccurs=
    "1" > <xsd:complexType> <xsd:sequence> <xsd:element name=
    "ingredient" type=
    "ingredientType" minOccurs=
    "1" maxOccurs=
    "unbounded" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=
    "atmosphere" minOccurs=
    "1" maxOccurs=
    "1" > <xsd:complexType> <xsd:sequence> <xsd:element name=
    "ingredient" type=
    "ingredientType" minOccurs=
    "1" maxOccurs=
    "unbounded" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=
    "temperatures" minOccurs=
    "1" maxOccurs=
    "1" > <xsd:complexType mixed=
    "true"> <xsd:sequence> <xsd:element name=
    "minimum" type=
    "temperatureType" minOccurs=
    "1" maxOccurs=
    "1" /> <xsd:element name=
    "maximum" type=
    "temperatureType" minOccurs=
    "1" maxOccurs=
    "1" /> <xsd:element name=
    "average" type=
    "temperatureType" minOccurs=
    "1" maxOccurs=
    "1" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=
    "diameter" minOccurs=
    "1" maxOccurs=
    "1" type=
    "diameterType" /> <xsd:element name=
    "gravitation" minOccurs=
    "1" maxOccurs=
    "1" type=
    "gravitationType" /> </xsd:all> <xsd:attribute name=
    "title" type=
    "xsd:string" use=
    "required" /> </xsd:complexType> <xsd:complexType name=
    "planetclassesType"> <xsd:sequence> <xsd:element name=
    "planetclass" type=
    "planetclassType" minOccurs=
    "1" maxOccurs=
    "unbounded" /> </xsd:sequence> </xsd:complexType>   <xsd:element name=
    "planetclasses" type=
    "planetclassesType" /> </xsd:schema>
    
  • SystemAdmin
    SystemAdmin
    1830 Posts

    Re: problem while validating xml using sax+xsd

    ‏2011-03-01T11:39:46Z  
    [2] I didn't look at your validation algorithm. Now that you mention that again, I see there is a problem with it.

    If you approach the validation with dom parser (rather than "sax" as the subject line might suggest), you have to make the builder factory namespace aware.
    
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
    //immediately after docBuilderFactor.setNamespaceAware(
    
    true);
    
  • Lul4n
    Lul4n
    3 Posts

    Re: problem while validating xml using sax+xsd

    ‏2011-03-01T12:13:47Z  
    [2] I didn't look at your validation algorithm. Now that you mention that again, I see there is a problem with it.

    If you approach the validation with dom parser (rather than "sax" as the subject line might suggest), you have to make the builder factory namespace aware.
    <pre class="jive-pre"> DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); //immediately after docBuilderFactor.setNamespaceAware( true); </pre>
    well your right, as i wrote this thread from my memory and missed that i changed the parser xD

    big thanks to you!
    if finally works!

    greez Julian