Basics of XSLT
XSLT documents are valid XML. XSLT requires an XML source document. Here’s a super, simple XML document :
<?xml version="1.0" encoding="ISO-8859-1"?>
<root/>
It only has a root element with no child elements and is about useless. If we wanted to transform this for some reason, the XSLT could look like this :
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<table>
</table>
</xsl:template>
</xsl:stylesheet>
The actual transformation would be the conversion of line 1 : XML doctype, note the encoding type differs from the XML file.
The XSLT file’s encoding is what will be used for the generated document. line 2 : The actual XSL declaration. Version is set to 2.0. If you find XSL/XSLT documents
on the internet check the version number, there’s some incompabilities between versions 1 and 2.
The line 3 : XSL directives are elements starting with XSL followed by the colon and the directive.
This is namespacing the xml document to prevent collisions. The important part here is if you see
a reference to a directive like line 4 : line 5: line 5 : Closes the You can test the above XML/XSLT online at this website : XSLT Online Test Tool Just enter the xml in the left hand textarea and the xslt in the right hand box and click ‘transform’ . The output
should be a simple XML document with Another option would be to link the XSL file from the XML document and view it in the browser. Can view a sample
file online : glossary sample XML/XSL file To add a stylesheet to an XML document add it to the XML document after the xml declaration: Then load it in a modern browser. If you use the example above, it will be a blank page. View Source and
it will show the Most of this is boilerplate code which is used to start an XSL document. If you wish to add a comment to an XSL file, use the standard XML comment formatting
. Not exciting.
This is the only lame example used; the other examples will actually do something. Here’s what each part does :
xmlns:xsl
defines the xsl
namespace IRI(Internationalized Resource Identifiers) .
This works similiar to the doctype for HTML.output
or if
, these must be preceded by the <xsl: .
xsl:output
tells the XSL processor the output to use. Some options are xml,html(xhtml), text.
Note the html is not html5.xsl:template
starts the processing of the XML document. This is called a stylesheet module
. A name
attribute can be used to identify the stylesheet module. There can be multiple modules
per XSLT document. The match
attribute` defines a valid XPath expression. In this case the root node
of the document. To effectively use XSLT, it will require some knowledge of XPath expressions.<table>
this is actual output for each node matched by the expression.
This tells it to replace the node with the table element.table
element, xsl:template
, xsl:stylesheet
tags and ends
the program.<table />
as the only element. (Note there must not be any
spaces before the XML declaration.)View in browser
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="name_of_xsl_file.xsl"?>`
<table />
tag as the only element.<!-- comment goes here -->
Other XSLT Posts