Note that there are some explanatory texts on larger screens.

plurals
  1. POXML Schemas (XSD> - Can a non-empty element have a type and an attribute?
    text
    copied!<p>i'm relatively new to XML Schemas and i've encountered a problem. I know that for an element to have an attribute assocaited with it, it must be a complex type. However I can only figure out how to associate an attribute with an empty element (See Example 1)...is it possible to assocaite an attribute with a non-empty elemnet and have a type declared for that element (See Example 2)? I'm using Visual Studio 2008 to write the schemas...when i declare the element as a complex type it tells me that I have to remove the type declaration from the element - see below.</p> <p>Example 1:</p> <pre><code>&lt;phone units = "grams" /&gt; </code></pre> <p>Example 2:</p> <pre><code>&lt;phone units = "grams"&gt;92&lt;/phone&gt; </code></pre> <p>Phone.XML</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;phone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XSD.xsd"&gt; &lt;make&gt;Nokia&lt;/make&gt; &lt;model&gt;N700&lt;/model&gt; &lt;code&gt;532/4329&lt;/code&gt; &lt;weight units ="grams"&gt;92&lt;/weight&gt; &lt;price&gt;49.99&lt;/price&gt; &lt;/phone&gt; </code></pre> <p>XSD.xsd</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;xs:attribute name ="units" type ="xs:string" /&gt; &lt;xs:element name ="phone"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name ="make" type="xs:string" /&gt; &lt;xs:element name ="model" type="xs:string" /&gt; &lt;xs:element name ="code" type="xs:string" /&gt; &lt;xs:element name ="weight" type="xs:string"&gt; &lt;xs:complexType&gt; &lt;xs:attribute name="units" type="xs:string" /&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name ="price" type="xs:double" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;/xs:schema&gt; </code></pre> <p>I have yet to find an example on the net demonstrating this. Is it possible or do I have to sacrifice the type associated with the weigh element in order to insert the attribute?</p> <p>Any help would be much appreciated....Thanks....</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