Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you require is a <a href="http://www.w3schools.com/Schema/el_choice.asp" rel="nofollow">choice element</a> in your schema. It'll allow you to express that only one from a set of elements may appear.</p> <p>EDIT: you'll need to specify a name for the element, either directly via <code>name</code> or in reference to an element defined elsewhere via <code>ref</code>.</p> <p>Your only alternative <strong>as far as I know</strong> is to have something like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt; &lt;xs:element name="PetCage"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="animal" type="Animal" minOccurs="1" maxOccurs="1"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:complexType name="Animal" abstract="true"&gt; &lt;xs:attribute name="Name" type="xs:string" /&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="Cat"&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="Animal"&gt;&lt;/xs:extension&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>This would validate XML files of a form like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;PetCage&gt; &lt;animal xsi:type="Cat" Name="String"/&gt; &lt;/PetCage&gt; </code></pre> <p>Unfortunately this leaves you tied to that <code>animal</code> element name. I doubt that XML Schema allows you to specify a specific type and lets you use a wildcard for the name. XML Schema validates based on element names and, if the element is defined with an abstract type, the <code>xsi:type</code> attribute. There is an <code>xs:any</code> declaration, but that can't be tied to any specific type. The following will validate as best as possible, but still allows any element defined under the schema root to be used:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt; &lt;xs:element name="PetCage"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:any processContents="strict" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:complexType name="Animal" abstract="true"&gt; &lt;xs:attribute name="Name" type="xs:string" /&gt; &lt;/xs:complexType&gt; &lt;xs:element name="cat" /&gt; &lt;xs:complexType name="Cat"&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="Animal"&gt;&lt;/xs:extension&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>It would validate this correctly:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;PetCage&gt; &lt;cat Name="snookums"/&gt; &lt;/PetCage&gt; </code></pre> <p>... but it wouldn't stop you from putting a PetCage in a PetCage.</p> <p>I'm hoping someone with good knowledge of the XML Schema spec can confirm that this is correct or, preferably, offer the desired solution.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload