Adding Attributes to Elements with Simple Content

Contact Us or call 1-877-932-8228
Adding Attributes to Elements with Simple Content

Adding Attributes to Elements with Simple Content

An element with simple content is one that only contains character data. If such an element contains one or more attributes, then it is a complex-type element. Elements with simple content and attributes are declared using the xs:simpleContent element and then extending the element with the xs:extension element, which must specify the type of simple content contained with the base attribute. The syntax is shown below.

Syntax

<xs:element name="ElementName">
	<xs:complexType>
		<xs:simpleContent>
			<xs:extension base="xs:string">
				<xs:attribute name="AttName" type="xs:string"/>
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
</xs:element>

For example, the FirstName element in the XML instance below contains only simple content and has a single attribute. Below the instance is the snippet from the Author3.xsd schema that declares the FirstName element.

Code Sample:

Attributes/Demos/NatHawthorne.xml
<?xml version="1.0"?>
<Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Author3.xsd">
	<Name Pseudonym="true" HomePage="http://www.nathanielhawthorne.com">
		<FirstName Full="false">Nat</FirstName>
		<LastName>Hawthorne</LastName>
	</Name>
</Author>

Code Sample:

Attributes/Demos/Author3.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
---- C O D E   O M I T T E D ----

							<xs:element name="FirstName">
								<xs:complexType>
									<xs:simpleContent>
										<xs:extension base="xs:string">
											<xs:attribute name="Full" type="xs:boolean"/>
										</xs:extension>
									</xs:simpleContent>
								</xs:complexType>
							</xs:element>
---- C O D E   O M I T T E D ----

</xs:schema>
Next