Creating an Empty Element with Attributes

Our next step is to create an attribute for our empty element. To do that, we declare what XML Schema refers to as a complex type:

<?xml version="1.0" encoding="UTF-8"?>
<!-- empty2.xsd -->
<xs:schema 
  xmlns="http://www.oreilly.com/xslt"
  targetNamespace="http://www.oreilly.com/xslt"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="empty2">
    <xs:complexType>
      <xs:attribute name="color"/>
    </xs:complexType>
  </xs:element>

</xs:schema>

The XML document is slightly more interesting now:

<?xml version="1.0" encoding="utf-8"?>
<!-- empty2.xml -->
<empty2
  xmlns="http://www.oreilly.com/xslt"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.oreilly.com/xslt empty2.xsd"
  color="blue"/>

We simply declared an attribute with a name here. We could have declared a datatype for the attribute; we’ll do that soon.

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.