Jump to content

XML Validation Error


Guest meep
 Share

Recommended Posts

Hi, I've just started learning about XML and XML schema and I when I was trying to validate my XML against my schema, I got the following error:cvc-elt.1: Cannot find the declaration of element 'top10'.May I know if I did something wrong? I've check my schema and it seems to be valid so I was thinking it could be my XML. I've read tutorials from many sites and did quite abit of search on the error but I'm still lost as to where I'm committing a mistake :) Been at this for almost 9hours now.. Would really appreciate any advice!XSD

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://www.w3schools.com"elementFormDefault="qualified"><xsd:element name="top10">	<xsd:complexType>	  <xsd:sequence>	<xsd:element name="song">		<xsd:complexType>			<xsd:sequence>		<xsd:element name="title" type="xsd:string"/>		<xsd:element name="link" type="xsd:string"/>		<xsd:element name="description" type="xsd:string"/>			</xsd:sequence>		</xsd:complexType>	</xsd:element>	  </xsd:sequence>	</xsd:complexType></xsd:element>

XML

<?xml version="1.0" encoding="utf-8"?><top10 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sg="test" xsi:schemaLocation="http://www.w3schools.com http://ivle.informatics.unimelb.edu.au/serve/ykgchua/Informatics2/Stage1/1.3_rss-schema.xsd"><sg:song><sg:title>1. Crush - David Archuleta</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=287347170&id=287347122&s=143441&v0=9990</sg:link><sg:description>Crush by David Archuleta from the album Crush - Single</sg:description></sg:song><sg:song><sg:title>2. Change - Taylor Swift</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=285351890&id=285351858&s=143441&v0=9990</sg:link><sg:description>Change by Taylor Swift from the album Change - Single</sg:description></sg:song><sg:song><sg:title>3. Dreamer - Chris Brown</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=286449611&id=286449599&s=143441&v0=9990</sg:link><sg:description>Dreamer by Chris Brown from the album Dreamer - Single</sg:description></sg:song><sg:song><sg:title>4. Disturbia - Rihanna</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=282494359&id=282494340&s=143441&v0=9990</sg:link><sg:description>Disturbia by Rihanna from the album Good Girl Gone Bad: Reloaded</sg:description></sg:song><sg:song><sg:title>5. Paper Planes - M.I.A.</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=262161877&id=262161787&s=143441&v0=9990</sg:link><sg:description>Paper Planes by M.I.A. from the album Kala (Bonus Track Version)</sg:description></sg:song><sg:song><sg:title>6. American Boy (feat. Kanye West) - Estelle</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=275421074&id=275421056&s=143441&v0=9990</sg:link><sg:description>American Boy (feat. Kanye West) by Estelle from the album Shine</sg:description></sg:song><sg:song><sg:title>7. Viva la Vida - Coldplay</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=282656467&id=282656418&s=143441&v0=9990</sg:link><sg:description>Viva la Vida by Coldplay from the album Viva la Vida</sg:description></sg:song><sg:song><sg:title>8. Burnin' Up - Jonas Brothers</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=282915416&id=282915380&s=143441&v0=9990</sg:link><sg:description>Burnin' Up by Jonas Brothers from the album Burnin' Up - Single</sg:description></sg:song><sg:song><sg:title>9. Get Back - Demi Lovato</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=286626795&id=286626762&s=143441&v0=9990</sg:link><sg:description>Get Back by Demi Lovato from the album Get Back - Single</sg:description></sg:song><sg:song><sg:title>10. My Life - The Game & Lil Wayne</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=286595847&id=286595843&s=143441&v0=9990</sg:link><sg:description>My Life by The Game & Lil Wayne from the album My Life - Single</sg:description></sg:song></top10>

Link to comment
Share on other sites

You have your namespaces mixed up.First stop, I'm not completely sure whether your schema targets elements in a no namespace, or in the "http://www.w3schools.com" namespace... by the looks of it, it seems it targets "http://www.w3schools.com". Schema has a targetNamespace attribute that can explicitly specifies the target namespace, though this will not be a remedy for your case.In addition, the top10 in your XML is not in a namespace. It must be in the namespace the schema targets ("http://www.w3schools.com"). Your "song" elements are in the "test" namespace. Even if you adjust the top10 element, the document still won't validate because of the "song" elements.The remedy here is to place all elements in the "http://www.w3schools.com" namespace, like this:

<?xml version="1.0" encoding="utf-8"?><sg:top10 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sg="http://www.w3schools.com" xsi:schemaLocation="http://www.w3schools.com http://ivle.informatics.unimelb.edu.au/serve/ykgchua/Informatics2/Stage1/1.3_rss-schema.xsd"><sg:song><sg:title>1. Crush - David Archuleta</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=287347170&id=287347122&s=143441&v0=9990</sg:link><sg:description>Crush by David Archuleta from the album Crush - Single</sg:description></sg:song><sg:song><sg:title>2. Change - Taylor Swift</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=285351890&id=285351858&s=143441&v0=9990</sg:link><sg:description>Change by Taylor Swift from the album Change - Single</sg:description></sg:song><sg:song><sg:title>3. Dreamer - Chris Brown</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=286449611&id=286449599&s=143441&v0=9990</sg:link><sg:description>Dreamer by Chris Brown from the album Dreamer - Single</sg:description></sg:song><sg:song><sg:title>4. Disturbia - Rihanna</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=282494359&id=282494340&s=143441&v0=9990</sg:link><sg:description>Disturbia by Rihanna from the album Good Girl Gone Bad: Reloaded</sg:description></sg:song><sg:song><sg:title>5. Paper Planes - M.I.A.</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=262161877&id=262161787&s=143441&v0=9990</sg:link><sg:description>Paper Planes by M.I.A. from the album Kala (Bonus Track Version)</sg:description></sg:song><sg:song><sg:title>6. American Boy (feat. Kanye West) - Estelle</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=275421074&id=275421056&s=143441&v0=9990</sg:link><sg:description>American Boy (feat. Kanye West) by Estelle from the album Shine</sg:description></sg:song><sg:song><sg:title>7. Viva la Vida - Coldplay</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=282656467&id=282656418&s=143441&v0=9990</sg:link><sg:description>Viva la Vida by Coldplay from the album Viva la Vida</sg:description></sg:song><sg:song><sg:title>8. Burnin' Up - Jonas Brothers</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=282915416&id=282915380&s=143441&v0=9990</sg:link><sg:description>Burnin' Up by Jonas Brothers from the album Burnin' Up - Single</sg:description></sg:song><sg:song><sg:title>9. Get Back - Demi Lovato</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=286626795&id=286626762&s=143441&v0=9990</sg:link><sg:description>Get Back by Demi Lovato from the album Get Back - Single</sg:description></sg:song><sg:song><sg:title>10. My Life - The Game & Lil Wayne</sg:title><sg:link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=286595847&id=286595843&s=143441&v0=9990</sg:link><sg:description>My Life by The Game & Lil Wayne from the album My Life - Single</sg:description></sg:song></top10>

or if you want, you can omit the "sg" prefix like so:

<?xml version="1.0" encoding="utf-8"?><top10 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3schools.com" xsi:schemaLocation="http://www.w3schools.com http://ivle.informatics.unimelb.edu.au/serve/ykgchua/Informatics2/Stage1/1.3_rss-schema.xsd"><song><title>1. Crush - David Archuleta</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=287347170&id=287347122&s=143441&v0=9990</link><description>Crush by David Archuleta from the album Crush - Single</description></song><song><title>2. Change - Taylor Swift</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=285351890&id=285351858&s=143441&v0=9990</link><description>Change by Taylor Swift from the album Change - Single</description></song><song><title>3. Dreamer - Chris Brown</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=286449611&id=286449599&s=143441&v0=9990</link><description>Dreamer by Chris Brown from the album Dreamer - Single</description></song><song><title>4. Disturbia - Rihanna</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=282494359&id=282494340&s=143441&v0=9990</link><description>Disturbia by Rihanna from the album Good Girl Gone Bad: Reloaded</description></song><song><title>5. Paper Planes - M.I.A.</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=262161877&id=262161787&s=143441&v0=9990</link><description>Paper Planes by M.I.A. from the album Kala (Bonus Track Version)</description></song><song><title>6. American Boy (feat. Kanye West) - Estelle</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=275421074&id=275421056&s=143441&v0=9990</link><description>American Boy (feat. Kanye West) by Estelle from the album Shine</description></song><song><title>7. Viva la Vida - Coldplay</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=282656467&id=282656418&s=143441&v0=9990</link><description>Viva la Vida by Coldplay from the album Viva la Vida</description></song><song><title>8. Burnin' Up - Jonas Brothers</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=282915416&id=282915380&s=143441&v0=9990</link><description>Burnin' Up by Jonas Brothers from the album Burnin' Up - Single</description></song><song><title>9. Get Back - Demi Lovato</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=286626795&id=286626762&s=143441&v0=9990</link><description>Get Back by Demi Lovato from the album Get Back - Single</description></song><song><title>10. My Life - The Game & Lil Wayne</title><link>http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=286595847&id=286595843&s=143441&v0=9990</link><description>My Life by The Game & Lil Wayne from the album My Life - Single</description></song></top10>

Link to comment
Share on other sites

  • 1 month later...

Hello,I have the same error:org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'catalogue'.at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)at org.apache.xerces.jaxp.validation.XMLSchemaValidator.validate(Unknown Source)at javax.xml.validation.Validator.validate(Unknown Source)A part of my XSD schema:

<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">	<xsd:element name="catalogue">    <xsd:complexType>      <xsd:sequence>        <xsd:element ref="date_created"/>        <xsd:element ref="date_modified"/>        <xsd:element ref="comment"/>        <xsd:element ref="books"/>        <xsd:element ref="cds"/>      </xsd:sequence>    </xsd:complexType>  </xsd:element>

And a part of the XML file:

<?xml version="1.0" encoding="UTF-8"?><catalogue><date_created><day>1</day><month>7</month><year>2008</year></date_created><date_modified><day>1</day><month>7</month><year>2008</year></date_modified>

I try to validate a DOM Document using:

SchemaFactory factory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);Source schemaFile=new StreamSource("myschema.xsd");schema schema=factory.newSchema(schemaFile);Validator validator=schema.newValidator();validator.validate(new DOMSource(document));

I don't validate using the parse(File) method, because I already have a DOM Tree in memory.Does somebody know how I can solve this?Thank you,Katja

You have your namespaces mixed up.First stop, I'm not completely sure whether your schema targets elements in a no namespace, or in the "http://www.w3schools.com" namespace... by the looks of it, it seems it targets "http://www.w3schools.com". Schema has a targetNamespace attribute that can explicitly specifies the target namespace, though this will not be a remedy for your case......
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...