XSLT - XML Declaration: XSLT Code (Work in Progress!)
XSLT - XML Declaration: XSLT Code (Work in Progress!)
XSLT - XML Declaration: XSLT Code (Work in Progress!)
Every XSLT file must have the root element xsl:stylesheet. This root
element has two attributes that must be included:
If you want to follow along on your own computer, copy and paste that text
and save the file as class.xsl.
You do not need to know the intricacies of the xsl:stylesheet to use XSLT.
What you do need to remember is to include both the XML declaration and the
root element in your XSLT code!
As you probably noticed, the root element specifies the XSL namespace.
This basically means you have to put a xsl: prefix before every XSL element.
Although this may seem annoying, it is absolutely required, so you better get
used to it! The standard form of an XSL element is:
xsl:element
The rest of XSLT is just learning the special elements and attributes that
XSLT uses. The next few lessons will talk about the most important XSLT
elements and their related attributes.
xslt- introduction
XML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="class.xsl"?>
<class>
<student>Jack</student>
<student>Harry</student>
<student>Rebecca</student>
<teacher>Mr. Bean</teacher>
</class>
XSLT Code:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="teacher">
<p><u><xsl:value-of select="."/></u></p>
</xsl:template>
<xsl:template match="student">
<p><b><xsl:value-of select="."/></b></p>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The XML file class.xml is linked to the XSLT code by adding the xml-
stylesheet reference. The XSLT code then applies its rules to transform the
XML document.
However, if you were to "view the source" of these XML files in your
browser, you would just see the XML document and not the transformed file.
XSLT does not change an XML document, but this example shows how XSLT
can be used to temporarily manipulate XML.
Below, we have manually reconstructed the XSLT output that you see when
you click class.xml. This HTML was created from an XML document bring
transformed by our XSLT code.
<p><b>Jack</b></p>
<p><b>Harry</b></p>
<p><b>Rebecca</b></p>
<p><u>Mr. Bean</u></p>
</body>
</html>
As you can see, we used XSLT to convert the XML document into a simple
webpage. Because we are an internet-related website, all of our XSLT lessons
will be focusing on browser ready XSLT output. However, you can pretty much
do anything with XSLT!
xslt- xsl:template
Both of these items are taken care of with the very important XSL
element xsl:template.
XML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="class.xsl"?>
<class>
<student>Jack</student>
<student>Harry</student>
<student>Rebecca</student>
<teacher>Mr. Bean</teacher>
</class>
Every time we find a student, let's print something out. The text we want
printed must go between the opening and closing tags of <xsl:template>. Let's
have it print out, "Found a learner!"
The following XSLT code will find student elements in the XML and
output, "Found a learner!" for each student element. This example displays both
the finding and the adding text functionality of XSLT!
This simple XSLT does not work in Firefox because the output is not well-
formed XML. However, Internet Explorer 6.0+ will view it just fine. Here is the
beginning of our XSLT transformation: class1.xml.
If your browser doesn't like the class1.xml file, this is what an XSLT
processor or Internet Explorer 6.0+ would show you:
XSLT Output:
Found a learner! Found a learner! Found a learner! Mr. Bean
Notice that this output doesn't have a root element, which is a requirement
for a well-formed XML document. Our XML document had three students and
one teacher, so our XSLT printed out, "Found a learner!" three times, followed
by the unmatched teacher element's text "Mr. Bean".
In a later lesson, we will show you how to filter out the unwanted text, "Mr.
Bean".
We didn't create a well-formed XML document with this output, and will be
showing you how to do that in the following lessons. But this lesson is
important because it shows you how to find elements using the match attribute
and how to add text using XSLT.
xslt- xsl:value-of
Although using XSLT to print out static messages is fun to play around with,
using an XML document's elements and their contents is probably more useful
in real world use. This lesson will teach you how to get the value of elements
and attributes in an XML document using the xsl:value-of element.
We will once again be using the class.xml document.
XML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="class.xsl"?>
<class>
<student>Jack</student>
<student>Harry</student>
<student>Rebecca</student>
<teacher>Mr. Bean</teacher>
</class>
The highlighted code will return the value of the student element, which is a
student's name.
You can view the updated XML/XSLT file here: class4.xml. If you were
outputting the XSLT to a file, this is what the output would look like.
XSLT Output:
<html>
<body>
<p>Jack</p>
<p>Harry</p>
<p>Rebecca</p>
</body>
</html>
XML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="class.xsl"?>
<class>
<student id="1">Jack</student>
<student id="2">Harry</student>
<student id="3">Rebecca</student>
<teacher id="1">Mr. Bean</teacher>
</class>
Let's output each student's id number before their name to make a nice list.
To select an attribute, use the at sign "@", followed by the name of the attribute.
You can view our updated XML/XSLT code here: class-attr.xml. The XSLT
output is shown below:
XSLT Output:
<html>
<body>
<p>1 - Jack</p>
<p>2 - Harry</p>
<p>3 - Rebecca</p>
</body>
</html>