Skip to content Skip to navigation

OpenStax-CNX

You are here: Home » Content » XSLT identity transform

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

In these lenses

  • XSLT display tagshide tags

    This module is included inLens: Hannes Hirzel's XSLT Lens
    By: Hannes Hirzel

    Comments:

    "Explains the built in default transformation rules. An identity style sheet is a point of departure for you own style sheet."

    Click the "XSLT" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

XSLT identity transform

Module by: Hannes Hirzel. E-mail the author

Summary: The module shows an XSLT transform which copies the input XML file to the output unchanged -- an identity transform.

Introduction

An XSLT processor may transforms an XML input data file into another XML data file, the result document.

XSLT processor with input and output file

As a starting point to write your own XSLT stylesheet, it is good to know how a style sheet looks like that does not change anything. That is an XSLT style sheet that just copies everything from the input file to the output file. We may call this identity transform.

In the following we will run different examples to get an idea how an identity transform works.

An "empty" XSLT file

Lets start with an XSLT file which is "empty", i.e in which you give no templates.

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
version="1.0">

<xsl:output method="xml" indent="yes" />



</xsl:stylesheet>

Sample XML input file

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

<p>Hello</p>
<p>World</p>

</root>

Result XML file

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

Hello
World

Interpretation

An XML file may have several types of nodes. In the input file we have element nodes (the <p> nodes) and text nodes (Hello and World). From the result we see that the text nodes are copied to the output file but not the element nodes. The default behavior seemingly is to copy the text nodes without any ado.

An XSLT file with a copy template for text nodes

Let's add a template to the empty style sheet which copies text nodes. The match expression for nodes is text() which means that it matches any text node. The select expression is for a dot which indicates the current node. As text nodes are leaf nodes there is no need for adding <xsl:apply-templates /> in the template. But if you do so nothing changes in the output as there are no children nodes.

Template which copies text nodes

<xsl:template match="text()">
<xsl:value-of select="." />
</xsl:template>

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
                version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="text()">
<xsl:value-of select="." />
</xsl:template>


</xsl:stylesheet>

Sample XML input file

<?xml version="1.0" encoding="UTF-8"?>
<root>
<p>Hello</p>
<p>World</p>
</root>

Result

<?xml version="1.0" encoding="utf-8"?>
Hello
World

Interpretation

As you might have expected the result is the same as above with the XSLT file which contained no template rule. The reason is that this text matching template is built in as the default behavior. What we did is to write out this template rule again in our style sheet. The default copying behaviour is seemingly is not the case for element nodes, so let's try to come up with a template which copies the element nodes.

An XSLT file with a "copy-of" template

As we have seen in the previous section the default behavior for text nodes is that they are copied. So let's go back to the original empty style sheet.

"Empty" style sheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
version="1.0">

<xsl:output method="xml" indent="yes" />



</xsl:stylesheet>

Now let's add a template which uses the copy-of. It will do a "deep-copy". This means it will copy the current node and all its descendants.

<xsl:template match="/">
<xsl:copy-of select="*"/>
</xsl:template>

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
                version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
<xsl:copy-of select="*"/>
</xsl:template>

</xsl:stylesheet>

Sample XML input file

<?xml version="1.0" encoding="UTF-8"?>
<root>
<p>Hello</p>
<p>World<b>!</b></p>
</root>

Result file

<?xml version="1.0" encoding="UTF-8"?>
<root>
<p>Hello</p>
<p>World<b>!</b></p>
</root>

Interpretation

The result file is indeed a copy of the input file. In the second <p> element node there is a <b> node which is copied as well.

Identity function XSLT

As we have seen in the previous section the simplest identity function matches the root node and copies all the subsequent element nodes. We have as well seen that the default behavior is to copy the text nodes so we do not have to indicate that.

Simple Identity Function

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
                version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
<xsl:copy-of select="*"/>
</xsl:template>

</xsl:stylesheet>

Note:

This first identity function uses copy-of ("deep copy"). This copies entire sub-trees. In this case it is applied to the whole document. The code in the next section uses a copy template which contains <xsl:apply-templates />.

Identity function using a copy template

The following identity XSLT file uses the <xsl:copy> instruction instead of the <xsl:copy-of>. For this to work it has to use the <xsl:apply-templates /> instruction. It copies attributes, processing instructions and comments as well.

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
                version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="*">
<xsl:copy><xsl:apply-templates select="*"/></xsl:copy>
</xsl:template>

</xsl:stylesheet>

Result

If we apply the stylesheet to the input file used before we get:


<?xml version="1.0" encoding="utf-8"?>
<root>
  <p />
  <p>
    <b />
  </p>
</root>

Interpretation

In this stylesheet xsl:apply-templates is used to have the processor continue on the child nodes after having found a match with *. However this only applies to element nodes so the text nodes are not copied anymode.

XPath expression

With an XPath expression of *|text() in the select part we make sure that further templates are applied to text nodes as well. In this case it will be the built-in default text node template which just copies them.

XSLT

The XSLT which the select expression changed so that it will copy the text nodes as well.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
                version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="*">
<xsl:copy><xsl:apply-templates select="*|text()"/></xsl:copy>
</xsl:template>

</xsl:stylesheet>

Result


<?xml version="1.0" encoding="utf-8"?>
<root>

<p>Hello</p>
<p>World<b>!</b></p>
</root>

Full copy template

The XSLT style sheet below contains an XPath expression which makes sure that all the nodes are copied.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
                version="1.0">

<xsl:output method="xml" indent="yes" />

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


</xsl:stylesheet>

Using the identity XSLT as a start

An identity template may be used as a start for writing your XSLT transformation if you basically want to copy everything form the input file to the output file while doing a few changes here and there.

Let's say we want to change every <p> element to a <paragraph> element. To do this we add the following template.

Transformation of p elements

<xsl:template match="p">
        <xsl:element name="paragraph">
            <xsl:apply-templates />
         </xsl:element>
</xsl:template>

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
                version="1.0">

<xsl:output method="xml" indent="yes" />

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


 <xsl:template match="p">
        <xsl:element name="paragraph">
            <xsl:apply-templates />
         </xsl:element>
</xsl:template>



</xsl:stylesheet>

Sample input XML file

<?xml version="1.0" encoding="UTF-8"?>
<root>
<!-- a comment -->
<p>Hello</p>
<p>World<b>!</b></p>
</root>

Result file

<?xml version="1.0" encoding="utf-8"?>
<root>
<!-- a comment -->
<paragraph>Hello</paragraph>
<paragraph>World<b>!</b></paragraph>
</root>

References

The transforms are taken from http://wwbota.free.fr/XSLT_models. That web page contains more example transforms.

Michael Kay, XSLT 2.0 and XPath 2.0, Programmer's reference, 4th ed, Wrox, p. 290 'identity template'.

The picture was taken from http://en.wikipedia.org/wiki/XSLT.

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks