The DevCenter has moved!

The Developer Center has a new home, developer.ektron.com. The site you are currently viewing is available as a read-only archive.
Things to note:
  1. Your username & password is the same on the new site, developer.ektron.com
  2. Going forward, please use the new URL, developer.ektron.com
  3. The old discussion forums and old KB articles are still available as an archive (read-only)

Ektron Knowledge Base

The Knowledge Base has thousands of articles describing open and past issues and providing answers to frequently asked questions.

Hover the mouse over to the side bar to open a side menu and expend a node for a list of articles.

INFO:XSLT: Identity templates

Applies To:
All Ektron Products
Summary:

XSL transforms are a very powerful way to convert XML data to HTML, XHTML, plain text. They are also excellent at filtering XML data or translating from one vocabulary (that is, a DTD or schema) to another. Writing the XSLT templates to perform the transformations, however, can be challenging. Just trying to figure out how to copy an XML document using XSLT (the identity transform) is a lot more difficult than you might expect.

Listed below are several templates that you may need when using eWebEditPro+XML. More Ektron knowledgebase articles on XSLT are available to give useful tips and help with common pitfalls. This article does not attempt to explain how XSLT templates or XPath pattern matching works. For an introduction to XSLT and XPath, please search the web or books on the subject. There are excellent resources readily available.

The Identity Transform

The most basic of all templates is the identity template or transform. This transform simply copies all the tags and attributes of the source document to the resulting document. The identity template is the basis for all transforms that filter the source by adding, removing or altering some tags and attributes, but passes the others through unchanged.

A word of caution, however, is appropriate. There are several aspects of an XML document that cannot be truly duplicated-at least not without significant effort. There are: the XML declaration, namespace nodes, the way empty tags are closed, and whitespace between tags.

The XML declaration

<?xml version="1.0" encoding="UTF-8"?>

The XML declaration is the "<?xml ..." line typically found at the top of the XML document. Technically, this line is not really part of the document, but provided meta information about the document itself. The omit-xml-declaration can be used to control whether the declaration is output or not. You cannot, however, copy the encoding of the source document. The encoding must be specified in the xsl:output tag in the XSLT and the xsl:output tag does not accept variables. The only way to dynamically set the output encoding is to write an XSLT that creates and XSLT.

The xsl:output tag in the XSLT controls the XML declaration of the resulting XML document.

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

Namespace nodes

<html xmlns="http://www.w3.org/1999/xhtml">

Namespace nodes appear to be attributes, but are not processed like attributes by the XSL transform. These namespace nodes can seem to materialize out of thin air when running an XSLT. Often you will want to eliminate them from your output document. The basic identity template will copy the namespace nodes and allow the XSLT to insert them where needed. However, two alternative templates are provided to replace the basic identity template that removes namespace nodes.

Empty tag closure

In XML, both of the following are valid empty tags.

  • <sometag />
  • <sometag></sometag>

Given that both are equivalent in XML, the XML parser treats them the same. Which style is used for output depends on how the template is written. The basic identity template will output both the opening and closing tags rather than using the short form. A second template can be added to use the short form instead. This may be preferred when producing XHTML so that tags like br, hr, and img do not have separate closing tags.

Whitespace between tags

The indentation and presentation of tags is controlled by the indent attribute of the xsl:output tag and the xsl:preserve-space and xsl:strip-space tags. Generally, preferred results are attained by setting indent="yes". If you are removing tags from the source document, the resulting document may have blank lines unless you include the xsl:strip-space tag. For example, <xsl:strip-space elements="*"/>.

Complete identity transform XSLT

This XSLT transform uses the basic identity template. It is provided as an example of how a template appears within an XSLT.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>

<!-- identity template -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

Basic identity template

This template copies all attributes (@*) and nodes, which include tags, text, comments and processing instructions. This template is commonly added to transforms that process an XHTML document that includes custom XML tags or an XML document that allows standard XHTML tags. It is also the basis of a filter that modifies an XML document. This template should be added to your XSLT if you want to copy most of the source document. Other templates that match more specific tags or attributes will take precedence over this template.

<!-- identity template -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

Identity templates that remove namespace nodes

Use these two template instead of the basic identity template to copy content without the namespace nodes.

<!-- identity template without namespace nodes -->
<xsl:template match="*">
    <xsl:element name="{name()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@*|text()|comment()|processing-instruction()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

Identity template to output short form for empty tags

Add this template to use the short form for empty tags, for example, <br />. This template removes namespace nodes. If you want namespace nodes, change the xsl:element tag to xsl:copy.

<xsl:templatematch="*[count(node())=0]">
    <xsl:element name="{name()}">
        <xsl:apply-templates select="@*"/>
    </xsl:element>
</xsl:template>

Identity templates for XHTML

Use these templates to use the short form for HTML tags that must be empty, for example, <br />. This template removes namespace nodes. If you want namespace nodes, change the xsl:element tag to xsl:copy.

<xsl:template match="*">
    <!-- identity with closing tags -->
    <xsl:element name="{name()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="area[not(node())]|bgsound[not(node())]|br[not(node())]|hr[not(node())]|img[not(node())]|input[not(node())]|param[not(node())]">
    <!-- identity without closing tags -->
    <xsl:element name="{name()}">
        <xsl:apply-templates select="@*"/>
    </xsl:element>
</xsl:template>

More Resources: