O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Abstract Elements and Datatypes

Our final topic is the use of abstract elements and datatypes. An abstract element or datatype works like an abstract class in object-oriented programming languages. We define the abstract class, but we have to create a subclass to actually use it. In terms of XML Schema, we’ll be creating new elements and datatypes based on the abstract ones.

In our first example, we’ll define an abstract element and its properties, and then use our substitution group as before. The only change to the schema is that we’ve added abstract="true" to the definition of the <address> element:

<?xml version="1.0" encoding="UTF-8"?>
<!-- abstract1.xsd -->
<xs:schema 
...
  <xs:element name="address" type="xs:string" abstract="true"/>

  <xs:element name="businessAddress" type="xs:string"
    substitutionGroup="address"/>
  <xs:element name="residentialAddress" type="xs:string"
    substitutionGroup="address"/>
...

The effect of the abstract element is that we’re forcing the document to contain either a <businessAddress> or a <residentialAddress>. This document is valid:

<?xml version="1.0" encoding="utf-8"?>
<!-- abstract1.xml -->
<person
  xmlns="http://www.oreilly.com/xslt"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.oreilly.com/xslt abstract1.xsd">
  <name>Doug Tidwell</name>
  <birthday>1965-06-19</birthday>
  <age>42</age>
  <businessAddress>4013 Corporate Parkway</businessAddress>
</person>

If this document used the <address> element instead of <businessAddress> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required