0

address.xml is as follows

<!--address.xml-->
<?xml version ="1.0" encoding="UTF-8"?>
<!DOCTYPE address SYSTEM "address.dtd">
<address 

    xmlns:personal="Personal things"
    xmlns:houses="Regarding to houses"  

    xmlns="http://www.w3schools.com" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
    xsd:schemaLocation="address.xsd"

>
    <name>  
        <personal:title>Mr.</personal:title>
        <first-name>Samitha</first-name>
        <last-name>Chathuranga</last-name>
    </name>

    <house-id>
        <houses:title>107 B</houses:title>
        <NAME>Sam&apos;s Home</NAME>
        <!--  An intnal entity is used for the single quote in House Name here-->
    </house-id>
    <village>Poramba</village>
    <city district="Galle" province="Southern">AG</city>
    <postal-code>80300</postal-code>
    <country>Sri Lanka</country>
</address>

The errors when validated using java SAX parser are,

"Attribute "xmlns" must be declared for element type "address". Attribute "xmlns:xsd" must be declared for element type "address". Attribute "xsd:schemaLocation" must be declared for element type "address"."

But if I removed the xsd referencing 3 lines, no errors come. Those lines are,

xmlns="http://www.w3schools.com" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
xsd:schemaLocation="address.xsd"
Samitha Chathuranga
  • 1,689
  • 5
  • 30
  • 57

1 Answers1

1

There are a number of problems to address:

  1. You are specifying both a DTD (via DOCTYPE) and an XML Schema (via xsd:schemaLocation).
  2. You are specifying an XML declaration (<?xml version ="1.0" encoding="UTF-8"?>), but it is not on the first line of the file.
  3. You are specifying a schemaLocation value that is not in terms of namespace-schema pairs.
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1)Can't we specify both DTD and an XML schema for a single XML file??? 2)Adding comments to the first line of an XML file is OK no? Eventhough there are any comments in the first lines the next first non-comment line is taken as the 1st line. – Samitha Chathuranga May 14 '14 at 15:18
  • Your 3rd issue is not clear.. What are namespace-schema pairs? So how is the correct way? – Samitha Chathuranga May 14 '14 at 15:23
  • Edwin Dankert Above code is taken from http://www.edankert.com/validate.html and there they have done the both referencings. – Samitha Chathuranga May 14 '14 at 15:27
  • (1) Yes, if it wasn't a simple oversight, care will have to be taken that the DTD and XSDs are compatible. (2) No, not before the XML dec. (3) Read [How schema definitions are located on the Web](http://www.w3.org/TR/xmlschema-1/#schema-loc) – kjhughes May 14 '14 at 15:38
  • do u mean we can't add comments before the xml declaration? That's quite strange.. I've seen many places where comments are added before xml declarations.. – Samitha Chathuranga May 14 '14 at 17:16
  • Right. For well-formed XML documents, only one XML declaration is permitted, and it **must be at the top** if used. See my answers [here](http://stackoverflow.com/a/20251895/290085) and [here](http://stackoverflow.com/a/19898942/290085) for more details. – kjhughes May 14 '14 at 17:31