views:

232

answers:

2

I want to define an XSD schema for an XML document, example below:

<?xml version="1.0" encoding="utf-8"?>
<view xmlns="http://localhost/model_data" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost/model_data XMLSchemaView.xsd" path="wibble" id="wibble">
    <text name="PageTitle">Homepage</text>
    <text name="Keywords">home foo bar</text>
    <image name="MainImage">
        <description>lolem ipsum</description>
        <title>i haz it</title>
        <url>/images/main-image.jpg</url>
        <type>image/jpeg</type>
        <alt>alt text for image</alt>
        <width>400</width>
        <height>300</height>
    </image>
    <link name="TermsAndConditionsLink">
        <url>/tnc.html</url>
        <title>Terms and Conditions</title>
        <target>_blank</target>
    </link>
</view>

There's a view root element and then an unknown number of field elements (of various types). I'm using the following XSD schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://localhost/model_data" targetNamespace="http://localhost/model_data" id="XMLSchema1">
    <xs:element name="view" type="model_data"/>
    <xs:complexType name="model_data">
        <xs:choice maxOccurs="unbounded">
            <xs:element name="text" type="text_field"/>
            <xs:element name="image" type="image_field"/>
            <xs:element name="link" type="link_field"/>
        </xs:choice>
        <xs:attribute name="path" type="xs:string"/>
        <xs:attribute name="id" type="xs:string"/>
    </xs:complexType>
    <xs:complexType name="image_field">
        <xs:all>
            <xs:element name="description" type="xs:string"/>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="type" type="xs:string"/>
            <xs:element name="url" type="xs:string"/>
            <xs:element name="alt" type="xs:string"/>
            <xs:element name="height" type="xs:int"/>
            <xs:element name="width" type="xs:int"/>
        </xs:all>
        <xs:attribute name="name" type="xs:string"/>
    </xs:complexType>
    <xs:complexType name="text_field">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="name" type="xs:string"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="link_field">
        <xs:all>
            <xs:element name="target" type="xs:string"/>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="url" type="xs:string"/>
        </xs:all>
        <xs:attribute name="name" type="xs:string"/>
    </xs:complexType>
</xs:schema>

This looks like it should work to me, but it doesn't and I always get the following error:

Element <text> is not allowed under element <view>.
Reason: The following elements are expected at this location (see below)
    <text>
    <image>
    <link>
Error location: view / text
Details
    cvc-model-group: Element <text> unexpected by type 'model_data' of element <view>.
    cvc-elt.5.2.1: The element <view> is not valid with respect to the actual type definition 'model_data'.

I've never really used XSD schemas before, so I'd really appreciate it if someone could point out where I'm going wrong.

A: 

You have multipication of names like 'text' (under 'model_data', and also under the schema). Just change one of them.

ofer shwartz
+2  A: 

UPDATE:

I think I found the problem - it's this statement here:

<view 
      xmlns="http://localhost/model_data" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 ==>  xsi:schemaLocation="http://localhost/model_data XMLSchemaView.xsd"  <====
      path="wibble" id="wibble">

You seem to refer to a location on your localhost machine here, referencing a XSD file - is that really there???

If I remove that one line from the XML, everything validates just fine.

Is this another left-over from some testing??

marc_s
Hi Marc,Thanks for pointing that out. That line was left over from me testing that the text element on its own would validate (it did). I've removed that line, but the document still won't validate.Sorry for the confusion there.
Matt
Looks like that's spot on. The weird thing is, that's a line that XMLSpy inserted automatically.Thanks for your help.
Matt