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.
Example 1:
<phone units = "grams" />
Example 2:
<phone units = "grams">92</phone>
Phone.XML
<?xml version="1.0" encoding="utf-8"?>
<phone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XSD.xsd">
<make>Nokia</make>
<model>N700</model>
<code>532/4329</code>
<weight units ="grams">92</weight>
<price>49.99</price>
</phone>
XSD.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name ="units" type ="xs:string" />
<xs:element name ="phone">
<xs:complexType>
<xs:sequence>
<xs:element name ="make" type="xs:string" />
<xs:element name ="model" type="xs:string" />
<xs:element name ="code" type="xs:string" />
<xs:element name ="weight" type="xs:string">
<xs:complexType>
<xs:attribute name="units" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name ="price" type="xs:double" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
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?
Any help would be much appreciated….Thanks….
Do something like this: