6.4. Generating entities

XSLT and entities are incompatible things: entities are expanded before running XSLT. Several methods are proposed to retain entities during XSLT processing. One of them is the following.

Each entity is represented by an element in some namespace and with the same local name as the entity name.

Example 16. Representing entities by elements

<?xml version="1.0"?>
<book xmlns:entity="http://xsieve.sourceforge.net/entity">  1
  <title>A small book</title>
  <entity:chapter1/>           2
  <entity:chapter2/>           3
</book>
1

The namespace prefix and URI are not important. The only requirement is correspondence between namespace declarations in XML and XSLT.

2

Placeholder for the entity chapter1.

3

Placeholder for the entity chapter2.

XSieve uses an extended SXML format that supports entities. The following XSieve program replaces the entity placeholders with the real entities.

Example 17. Converting placeholders to entities

<x:stylesheet
  xmlns:x = "http://www.w3.org/1999/XSL/Transform"
  xmlns:s = "http://xsieve.sourceforge.net"
  xmlns:entity = "http://xsieve.sourceforge.net/entity"  1
  extension-element-prefixes="s"
  version = "1.0">
<!-- -->

<x:template match="@*|node()">             2
  <x:copy>
    <x:apply-templates select="@*|node()"/>
  </x:copy>
</x:template>

<x:template match="entity:*">              3
  <s:scheme>
    (list '&amp; (x:eval "local-name()"))  4
  </s:scheme>
</x:template>

</x:stylesheet>
1

The namespace URI should be the same as in the XML file.

2

The identity transformation.

3

The rule matches all elements in the namespace.

4

To denote an entity, SXML uses the symbol &. Due to using XML syntax, the symbol should be written as &amp;.

Applying the program to the sample XML file gives the expected result.

Example 18. Real entities instead of placeholders

<?xml version="1.0"?>
<book xmlns:entity="http://xsieve.sourceforge.net/entity">
  <title>A small book</title>
  &chapter1;
  &chapter2;
</book>