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

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


Placeholder for the entity chapter1.


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

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

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

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


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


The identity transformation.


The rule matches all elements in the namespace.


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>