xs:choice unbounded list

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-04-07T10:10:35Z Indexed on 2010/04/07 10:13 UTC
Read the original article Hit count: 251

Filed under:
|
|
|

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="text" type="text_field"/>
    <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.

© Stack Overflow or respective owner

Related posts about xsd

Related posts about Xml