XSLT is also XML, write your first XSLT [June 16, 2012, 4:12 a.m.]
XSLT is also XML, write your first XSLT
XSLT is itself a XML file. XSLT is a programming language which can transform XML to all kind of different outputs e.g. HTML, XHTML, XML, TXT, PDF, SVG ….
That means that the output it self does not need to be XML valid.
Before we look at the structure of XSLT we need to look at one additional XML concept: Namespaces.
<?xml version="1.0" encoding="UTF-8"?> <root xmlns="mydefaultnamespace" xmlns:special="aspecialnamespace" > <child1 name=”Adam”/> <child2 name=”Eva”> <child1of2/> </child2> <!-- just a little comment --> <special:child1/> </root>
In the above XML we have a default namespace and a special namespace. Default namespace is applied to all tags which do not have defined namespace. So we have two tags which are named child1 but one has the default mydefaultnamespace and one the aspecialnamespace. So namespaces are to tags something like a family name to a person. They both have the “first name” child1 but one belongs to the “family name” mydefaultnamespace and the other one to aspecialnamespace.
Namespaces are important for complex XMLs and also for writing XSLT.
XSLT is XML with a special namespace
XSLT is specification which uses a special namespace for their commandos. For a XML transformation we always need a XML valid input file and a XSLT.
Let's take the very first XML as input file:
<?xml version="1.0" encoding="UTF-8"?> <hallo>world</hallo>
and now our first XSLT:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <the_world_in_xslt/> </xsl:template> </xsl:stylesheet>
Save them both to disk (e.g. input.xml and transform.xsl).
Use either xsltproc or saxon to transform them. I will show the simple xsltproc command:
xsltproc transform.xsl input.xml
You should see this result in the commandline:
<?xml version="1.0" encoding="UTF-8"?> <the_world_in_xslt/>
So let's take a deeper look at the XSLT. The XSLT starts with a xsl:stylesheet tag in which the xsl namespace is defined (the default XSLT namespace) and the version. Later in this course we will learn XSLT 2.0 but for now let's stick with 1.0.
Remember: All tags starting with xsl:..... are special XSLT commands. All other tags are no XSLT commands and will copied to the result XML output (the one you saw at the commandline).
xsl:output defines how the output file should look like. In our case it will be a xml file which is UTF-8 encoded and indented (indented means it is structured like a tree readable by human).
xsl:templates are the most important parts in a xslt. They are like procedures in other programming languages and they either match to a specific input node or can be called manually.
In our first XSLT we only have one template which matches to “/”. This is the root node. The root node is in any XML file but you will never see it because it is before the first node/tag of a XML.
Remember: Every XML has a root note. And just one!
So a template match to “/” is the ideal starting point for a XSLT.
The only thing the template does is to insert a new tag named <the_world_in_xslt/>. This tag has no xsl namespace so it will be copied to the resulting xml.
Remember: Tags and text nodes which are not inside the xsl namespace will be copied to the resulting XML!
Task:
Use the XML above and write an XSLT which gives you the following output when you process it with xsltproc:
<?xml version="1.0" encoding="UTF-8"?> <root> <firststeps> <myveryfirstxslt/> <looksgood/> </firststeps> </root>