XSLT examples
Requirements
- VS code extension installed.
- You have done the XPath tutorial.
The best way to understand what you can do with xslt is to use examples and practice. For this we have prepared examples for all levels.
First download this zip file containing all the examples: download examples
Extract all files in a new folder.
In VSCode add this folder to your workspace: right click on the left panel and click on Add folder to workspace...
You must have:
All transformation files transform.xslt are commented. Read the comments to learn how each example works.
To run an example, select the transform.xslt file and then on the bottom bar, click on Input to select the input file (select items.xml in the same folder), and then click Run transformation.
Best practice
To run a job use the shortcut Ctrl + Enter
The result will be generated in a file output.xml in the same folder.
For some examples you will have to change the output of the file generated (csv, json, html). This must be done in the xsl:output
element of the xslt file, and must be selected in the Output extension in the bottom bar.
Snippets
Snippets are a very fast way to write your xslt. In the example below, in a new document we are typing sty
to get the stylesheet snippet, then TAB key
three times to reach the third placeholder, and then for
to get a xsl:for-each
loop.
If snippets does not appear, ensure that:
- These snippets only exist on files with the extension
.xslt
. - Use the shortcut
Ctrl + Space
to display autocomplete - The language selected in the right bottom is XSL
All snippets
Best practice
Never write a xsl tag, use the snippets instead.
sty
Default stylesheet template
xml
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="fn"
xmlns:pl="http://product-live.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="3.0"
exclude-result-prefixes="xs fn pl">
<xsl:output method="${1|xml,text,json,html,xhtml,xslt|}" indent="${2|yes,no|}" encoding="UTF-8" cdata-section-elements="" />
<xsl:template match="/">
${3}
</xsl:template>
</xsl:stylesheet>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
for
For each loop
xml
<xsl:for-each select="${1}">
$2
</xsl:for-each>
1
2
3
2
3
fog
For each group loop
xml
<xsl:for-each-group select="${1}" group-by="${2}">
$3
</xsl:for-each-group>
1
2
3
2
3
if
If test. If you need multiple tests use Choose instead
xml
<xsl:if test="${1}">
$2
</xsl:if>
1
2
3
2
3
map
Map for Json
xml
<xsl:map>
$2
</xsl:map>
1
2
3
2
3
mape
Map-entry for Json
xml
<xsl:map-entry key="$1" select="$2"/>
1
mapem
Map-entry with map for Json
xml
<xsl:map-entry key="$1">
<xsl:map>
<xsl:map-entry key="$2" select="$3"/>
</xsl:map>
</xsl:map-entry>
1
2
3
4
5
2
3
4
5
ch
Choose test. Add many as when test as you need.
xml
<xsl:choose>
<xsl:when test="${1}">$2</xsl:when>
<xsl:otherwise>$3</xsl:otherwise>
</xsl:choose>
1
2
3
4
2
3
4
wh
When test must be inserted in Choose test.
xml
<xsl:when test="${1}">$2</xsl:when>
1
ot
Otherwise test must be inserted in Choose test.
xml
<xsl:otherwise>$1</xsl:otherwise>
1
var
Variable snippet (one line).
xml
<xsl:variable name="${1:name}" select="${2}" />
1
vari
Variable snippet (multiple lines).
xml
<xsl:variable name="${1:name}">
$2
</xsl:variable>
1
2
3
2
3
par
Param
xml
<xsl:param name="${1:name}" select="${2}" />
1
mes
Message snippet
xml
<xsl:message terminate="${1|no,yes|}">$2</xsl:message>
1
val
Value of snippet. See copy-of for sequence
xml
<xsl:value-of select="${1}"/>
1
cop
Copy of snippet. See value-of for text
xml
<xsl:copy-of select="${1}"/>
1
mer
Merge snippet
xml
<xsl:merge>
<xsl:merge-source name="${1:name1}" select="${2}" sort-before-merge="yes">
<xsl:merge-key select="${3}"/>
</xsl:merge-source>
<xsl:merge-source name="${4:name2}" select="${5}" sort-before-merge="yes">
<xsl:merge-key select="${3}"/>
</xsl:merge-source>
<xsl:merge-action>
$6
</xsl:merge-action>
</xsl:merge>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
te
Text snippet
xml
<xsl:text>$1</xsl:text>
1
cdata
CDATA snippet
xml
<![CDATA[$1]]>
1
cdata-txt
CDATA snippet
xml
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>$1<xsl:text disable-output-escaping="yes">]]></xsl:text>
1
fun
New function
xml
<xsl:function name="pl:name">
</xsl:function>
1
2
2
Performance
- Do not use
//
in your XPath - Never use the same XPath twice in your XSLT file, declare a variable instead
- Use built-in function http://www.xsltfunctions.com/
- Learn the
xsl:merge
function in the examples when you have to merge nodes
What you have learned
- XSLT is a very powerfull tool to transform data.
- The power of XPath is used to navigate data.
- XSLT has a lot of built-in functions: http://www.xsltfunctions.com/.