Stylus Studio XML Editor

Table of contents

Appendices

C Using Entities

Using Entities

XML 1.0 provides various types of entities which are named fragments of content that can be used in the construction of both DTD's (parameter entities) and instance documents. In [Building Content Models], we noted how named groups mimic parameter entities. In this section we show how entities can be declared in instance documents, and how the functional equivalents of entities can be declared in schemas.

Suppose we want to declare and use an entity in an instance document, and that document is also constrained by a schema. For example:

NOTE: 

Declaring and referencing an entity in an instance document.

<?xml version="1.0" ?>
<!DOCTYPE pPurchaseOrder [
<!ENTITY eacute "é&#xE9;">
]>
<purchaseOrder xmlns="http://www.example.com/PO1"
               orderDate="1999-10-20">
  <!-- etc. -->
    <city>Montr&eacute;al</city>
  <!-- etc. -->
</purchaseOrder>

Here, we declare an entity called eacute as part of an internal (DTD) subset, and we reference this entity in the content of the city element. Note that when this instance document is processed, the entity will be dereferenced resolved before schema validation takes place. In other words, a schema processor will determine the validity of the city element using Montréal as the element's value.

We can achieve a similar but not identical outcome by declaring an element in a schema, and by setting the element's content appropriately:

NOTE: 
<xsd:element name="eacute" type="xsd:token" fixed="é&#xE9;"/>

And this element can be used in an instance document:

NOTE: 

Using an element instead of an entity in an instance document.

<?xml version="1.0" ?>
<purchaseOrder xmlns="http://www.example.com/PO1"
               xmlns:c="http://www.example.com/characterElements"
               orderDate="1999-10-20">
  <!-- etc. -->
    <city>Montr<c:eacute/>al</city>
  <!-- etc. -->
</purchaseOrder>

In this case, a schema processor will process two elements, a city element, and an eacute element for the contents of which the processor will supply the single character é. Note that the extra element will complicate string matching; the two forms of the name "Montréal" given in the two examples above will not match each other using normal string-comparison techniques.