views:

705

answers:

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

A: 

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"&gt;        
  <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
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 <assignment id="a2" xmlns="http://www.levijackson.net/web340/ns">
Levi
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
What if at the top of the schema I declare rather than <xs:schema xlmns:xs...> just say <schema xmlns:...> then will that become the default schema for the page? For my other elements I would specify another schema in the opening <schema> 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
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
A: 

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
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
A: 

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