Creating an Empty Element

Here’s how we create an empty element:

<?xml version="1.0" encoding="UTF-8"?>
<!-- empty1.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="empty1">
    <xs:complexType/>
  </xs:element>

</xs:schema>

That’s it. The empty <xs:complexType> means that our element can’t have any attributes and it can’t have any content. It’s not terribly useful, but that’s how it works. An XML document that uses this schema looks like this:

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

Although we said our empty element couldn’t contain attributes, it looks like we have three of them here. The first two (xmlns and xmlns:xsi) are actually namespace declarations, not attributes. The xsi:schemaLocation attribute (it actually is an attribute) associates the XML Schema with our document. In the schema, the targetNamespace attribute defines the namespace used by this schema; it’s also the default namespace (xmlns="http://www.oreilly.com/xslt") in the schema and the XML document. To tie everything together, the xsi:schemaLocation attribute in our XML document tells the XML parser where to find the schema that defines what a valid document looks like.

There are other ways of using namespaces, but we won’t cover ...

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.