Linking XML with XSLT

Home » Scripting » XML » XSLT » Linking XML with XSLT

The first thing to know to understad this process is that there are two files, the XML document which contains the source data to be transformed and the XSLT style sheet document whichdescribes the rules of the transformation.

While XML can be transformed into nearly any format our example will return HTML.

To complete the XSLT transformation we will need an XSLT processor, fortunately all modern browsers have one and support XSLT ports, furthermore XML editors have built-in XSLT support.

An example on how to link the two files:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xsltstyle.xsl"?>

This is the begining of the XML file, the second line is the one linking the XML file to the XSLT, it works and has the same logic as the css file for html. So when the XML file is opened in a browser for example or an XSLT processor the XSLT transformation takes place before displaying the document.

Beginning an XSLT Style Sheet

An XSLT style sheet is an XML document that must be well-formed and it needs to begin with the XML declaratio, after the declaration we must define the W3C namespace for style sheets.

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


</xsl:stylesheet>

Code explanation:

  • to indicate that the XSLT style sheet is an XML document
  • to specify the namespace for the style sheet and declare its prefix ( xsl )
  • here we will add the instructions contained by the stylesheet
  • to complete the style sheet

Next we will learn how to create and add the instructions to the
stylesheet.