Je veux définir un schéma XSD pour un document XML, par exemple ci-dessous:xs: liste unbounded choix
<?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>
Il y a un élément racine de vue et un nombre inconnu d'éléments de champ (de différents types). J'utilise le schéma XSD suivant:
<?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>
Cela ressemble à cela devrait fonctionner pour moi, mais il ne fonctionne pas et je reçois toujours l'erreur suivante:
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'.
Je n'ai jamais vraiment J'ai déjà utilisé les schémas XSD, donc j'apprécierais vraiment que quelqu'un puisse me montrer où je me trompe.
Salut Marc, Merci d'avoir signalé cela. Cette ligne me restait à tester que l'élément de texte en lui-même validerait (il l'a fait). J'ai supprimé cette ligne, mais le document ne sera toujours pas validé. Désolé pour la confusion là-bas. – Matt
On dirait que c'est parfait. La chose étrange est, c'est une ligne que XMLSpy inséré automatiquement. Merci pour votre aide. – Matt