Simple XSL Transforms

Before we deploy our XSL servlet, let’s look at some simple transforms from the command line. First, a simple transform that detects the root node and then outputs the xalan environment. Naturally, this will only work when you’re using xalan .

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xalan="http://xml.apache.org/xalan"
exclude-result-prefixes="xalan">

<xsl:output method="xml"/>

<xsl:template match="/">
<xsl:copy-of select="xalan:checkEnvironment()"/>
</xsl:template>
</xsl:stylesheet>
From the command line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1 $ cat input.xml
<?xml version="1.0"?>
<root>
<child remove="attribute">
<grandchild>foo</grandchild>
</child>

<remove>
<alpha/>
</remove>
</root>

2 $ java org.apache.xalan.xslt.Process -IN input.xml -XSL metadata/xsl/environment.xsl
<?xml version="1.0" encoding="UTF-8"?><checkEnvironmentExtension>
<EnvironmentCheck version="$Revision$">
<environment>
<item key="version.DOM.draftlevel">2.0fd</item>
<item key="java.class.path">.;C:\Program Files\Java\jre1.5.0_16\lib\ext\QTJava.zip</item>

<item key="version.JAXP">1.1 or higher</item>
<item key="java.ext.dirs">C:\program files\Java\jdk1.5.0_16\jre\lib\ext</item>

<foundJar desc="apparent.version" name="serializer.jar">serializer.jar present-unknown-version</foundJar>
<foundJar desc="path" name="serializer.jar">C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\serializer.jar</foundJar>

<foundJar desc="path" name="xalan.jar">C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\xalan.jar</foundJar>
<foundJar desc="apparent.version" name="xercesImpl.jar">xercesImpl.jar WARNING.present-unknown-version</foundJar>
<foundJar desc="path" name="xercesImpl.jar">C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\xercesImpl.jar</foundJar>

<foundJar desc="apis.jar-apparent.version" name="xml">xml-apis.jar present-unknown-version</foundJar>
<foundJar desc="apis.jar-path" name="xml">C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\xml-apis.jar</foundJar>

<foundJar desc="apparent.version" name="xsltc.jar">xsltc.jar present-unknown-version</foundJar>
<foundJar desc="path" name="xsltc.jar">C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\xsltc.jar</foundJar>

<item key="version.xerces2">Xerces-J 2.9.0</item>
<item key="version.xerces1">not-present</item>

<item key="version.xalan2_2">Xalan Java 2.7.1</item>
<item key="version.xalan1">not-present</item>

<item key="version.ant">not-present</item>
<item key="java.version">1.5.0_16</item>

<item key="version.DOM">2.0</item>
<item key="version.crimson">not-present</item>

<item key="sun.boot.class.path">C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\serializer.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\xalan.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\xercesImpl.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\xml-apis.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\endorsed\xsltc.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\rt.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\i18n.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\sunrsasign.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\jsse.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\jce.jar;C:\program files\Java\jdk1.5.0_16\jre\lib\charsets.jar;C:\program files\Java\jdk1.5.0_16\jre\classes</item>
<item key="version.SAX">2.0</item>

<item key="version.xalan2x">Xalan Java 2.7.1</item>
</environment>

<status result="OK"/>
</EnvironmentCheck>
</checkEnvironmentExtension>

Next, let’s look at the “identity” transform. There is a single template directive that matches on attributes or elements (@* and node() respectively). For each attribute or element entity, we copy the current attribute or element and then we recursively apply the template to any children of the current element.

lang: xml
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
From the command line:
1
3 $ java org.apache.xalan.xslt.Process -IN input.xml -XSL metadata/xsl/echo.xsl
<?xml version="1.0"?>
<root>
  <child remove="attribute">
    <grandchild>foo</grandchild>
  </child>
  <remove>
    <alpha/>
  </remove>
</root>
Note that this simple transform only copies attributes and elements. Other entities, such as comments, will be ignored. And finally, a transform that removes any element named “remove” along with its children. We start with the identity transform, adding a priority attribute to the first template directive. We add a second template directive that only matches elements named “remove” with a higher priority value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>

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

<xsl:template match="remove" priority="20">
</xsl:template>

</xsl:stylesheet>
From the command line:
1
4 $ java org.apache.xalan.xslt.Process -IN input.xml -XSL metadata/xsl/remove.xsl
<?xml version="1.0" encoding="UTF-8"?><root>
  <child remove="attribute">
    <grandchild>foo</grandchild>
  </child>

</root>
If you change the match attribute from “remove” to “@remove”, then attributes named remove will be eliminated rather than elements. And using a match attribute of “@remove|remove” will eliminate both attributes and elements named “remove”.

Sep 12: Adding a REST Service to SoapUI

Oct 2: Using XSL Param