3

So I am beginning with XML and Schemas and I ran across this today and I have not been able to figure it out.

I am getting and error that says,

Ln 5 Col 2 : Cannot find the declaration of element 'assignments'.

I believe I have declared the element, but perhaps I am missing something and have not.

This is my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<assignments
    xmlns="http://www.w3.org/2001/XMLSchema-instance"
    SchemaLocation="A3.xsd"
>
    <assignment id="a1">
        <name>Schemas</name>
        <page>110</page>
    </assignment>

    <assignment id="a2">
        <name>Namespaces</name>
        <page>258</page>
        <files>names.xml</files>
        <files>names.dtd</files>
    </assignment>

    <assignment id="a3">
        <name>RELAX NG</name>
        <page>305</page>
        <files>account.xml</files>
        <files>customers.xml</files>
        <files>finance.xsd</files>
    </assignment>

</assignments>

This is my Schema file:

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    xmlns:target="http://www.levijackson.net/web340/ns" 
    targetNamespace="http://www.levijackson.net/web340/ns" elementFormDefault="qualified"
>
<element name="assignments" type="target:TypeAssignments"></element>

<complexType name="TypeAssignments">
    <sequence>
        <element name="assignment" type="target:assignmentInfo"></element>
    </sequence>
    <attribute name="id" type="string" use="required"/>
</complexType>

<complexType name="assignmentInfo">
    <sequence>
            <element name="name" type="string"></element>
            <element name="page" type="target:TypePage"></element>
            <element name="file" type="target:TypeFile" minOccurs="0" maxOccurs="unbounded"></element>
    </sequence>
</complexType>

<simpleType name="TypePage">
    <restriction base="integer">
        <minInclusive value="50" />
        <maxInclusive value="498" />
    </restriction>
</simpleType>

<simpleType name="TypeFile">
    <restriction base="string">
        <enumeration value=".xml" />
        <enumeration value=".dtd" />
        <enumeration value=".xsd" />
    </restriction>
</simpleType>

</schema>

As I am still learning, feel free to point out any other mistakes I may have made not related to the problem.

Thanks
Levi

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Levi
  • 12,214
  • 14
  • 43
  • 47
  • 1
    You shouldn't assign http://www.w3.org/2001/XMLSchema-instance as your default namespace, because then it is considered to be the namespace for all elements in your XML whose namespace hasn't been explicitly specified. Assign http://www.w3.org/2001/XMLSchema-instance it to a different namespace, like the commonly used xmlns:xsi. – Joren Sep 20 '09 at 00:05
  • If I set it up like that, will I need to specify an element type like this: type="xsi:string"? – Levi Sep 20 '09 at 00:09
  • 1
    Yes. By the way, in XSD schema's usually xs is used, and xsi in the XML files. – Joren Sep 20 '09 at 00:19

3 Answers3

2

The solution to this problem was that I was not declaring my main element 'assignments' as a complex element, I actually wasn't declaring it as anything at all.

So by taking this line from my schema file:

<element name="assignment" type="target:assignmentInfo" minOccurs="0" maxOccurs="unbounded"></element>

and changing it into this:

<element name="assignments">
    <complexType>
        <sequence>
            <element name="assignment" type="target:assignmentInfo" minOccurs="0" maxOccurs="unbounded"></element>
        </sequence>
    </complexType>
</element>

The element was properly defined, thanks for the help everyone.

Levi

Levi
  • 12,214
  • 14
  • 43
  • 47
0

Try the following. I took out some of the extra details of the schema element, but with a little tweaking something like the following should work:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">        
  <xs:element name="assignments" type="TypeAssignments" />    
  <xs:complexType name="TypeAssignments">
    <xs:sequence>
      <xs:element name="assignment" type="assignmentInfo" 
          minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>        
  </xs:complexType>

  <xs:complexType name="assignmentInfo">
    <xs:sequence>
      <xs:element name="name" type="xs:string" />
      <xs:element name="page" type="TypePage" />
      <xs:element name="files" type="xs:string"  minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:string" use="required" />
  </xs:complexType>   

  <xs:simpleType name="TypePage">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="50" />
      <xs:maxInclusive value="498" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="TypeFile">
    <xs:restriction base="xs:string">
      <xs:enumeration value=".xml" />
      <xs:enumeration value=".dtd" />
      <xs:enumeration value=".xsd" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

Notes:

  1. The ID attribute has been moved from TypeAssignments to assignmentInfo.
  2. All types and element names have been prefixed with the namespace xs
  3. I've added the minOccurs and maxOccurs attributes for the assignment elements. Otherwise, only a single assignment element is allowed within assignments.
  4. I've auto-closed your elements using "/>" instead of "" Not necessary, but a bit less typing to do.

All in all, I didn't modify all that much so you have the right idea.

David Andres
  • 31,351
  • 7
  • 46
  • 36
  • I understand why using the xs: prefix as a namespace is used. Would it be easier for me to declare a namespace as being used in the xml file so I don't have to prefix all of the elements in the schema with xs:? So if I say something like – Levi Sep 20 '09 at 01:27
  • 2
    Unfortunately, you need to prefix the elements with xs because the types (e.g., string, element, enumeration, etc.) are defined as part of separate schema. – David Andres Sep 20 '09 at 01:31
  • What if at the top of the schema I declare rather than just say then will that become the default schema for the page? For my other elements I would specify another schema in the opening tag like so : xmlns:target="http://www.levijackson.net/web340/ns" targetNamespace="http://www.levijackson.net/web340/ns" elementFormDefault="qualified" and then use target:assignmentInfo and so forth for the attribute values I created. – Levi Sep 20 '09 at 01:44
  • I tried this, by setting the the top level xmlns:xs="..." attribute to xmlns="..." It seems to work, but it will get messy when referring to your target namespace. – David Andres Sep 20 '09 at 01:56
0

In your instance document, as excerpted above, you define the xmlns attribute on the <assignments> element, and do not elsewhere define a namespace. This means that the namespace of <assignments> and all of its descendents is set to "http://www.w3.org/2001/XMLSchema-instance".

Your schema document, however, specifies a targetNamespace value of "http://www.levijackson.net/web340/ns". Since the elements in your instance document does not have this namespace, validation fails.

Start by changing your instance document to look like this:

<assignments
    xmlns="http://www.levijackson.net/web340/ns"
kdgregory
  • 38,754
  • 10
  • 77
  • 102
  • I am not sure what you are telling me I should do, what you pasted is what I have above. Also, the targetNamespace is referring to the the second namespace I declare right above it. Unless I am mistaken as to what/how the targetNamespace works. – Levi Sep 22 '09 at 23:03