Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

XSLT - XML Declaration: XSLT Code (Work in Progress!)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

xslt - xml declaration

This is more of a technicality, but you should include an XML declaration at


the top of your XSLT documents. The attribute version defines what version of
XML you are using.

XSLT Code (Work in Progress!):


<?xml version="1.0" ?>

xslt- stylesheet root element

Every XSLT file must have the root element xsl:stylesheet. This root
element has two attributes that must be included:

 version - the version of XSLT


 xmlns:xsl - the XSLT namespace, which is a URI to w3.org

As you probably guessed, an XSLT file is a well-formed XML document.

XSLT Code (Work in Progress!):


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

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!

xslt- xsl: namespace prefix

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

xslt- syntax overview


In this lesson, you learned about the required pieces for valid XSLT code.
You also learned that every XSLT element has an xsl: prefix to specify its
namespace.

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

XSLT is an XML-related technology that is used to manipulate and


transform XML documents. The acronym XSLT stands
for Extensible Stylesheet Language Transformations,

CSS is a way of styling HTML. In a similar relationship, XSLT is used to


style and transform XML.

we've provided a simple XML document that gets transformed by a complicated


looking XSLT file

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.

 Before XSLT: classoriginal.xml


 After XSLT rules are applied: class.xml

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.

XSLT Output (What you see in your browser):


<html>
<body>

<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

The purpose of XSLT is to help transform an XML document into


something new. To transform an XML document, XSLT must be able to do two
things well:

1. Find information in the XML document.


2. Add additional text and/or data. In a previous example, we added HTML
tags.

Both of these items are taken care of with the very important XSL
element xsl:template.

xslt- xsl:template match attribute

To find information in an XML document you will need to use


xsl:template's match attribute. It is in this attribute that you use your knowledge
of XPath to find information in your XML document.

We will be using class.xml as our example 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>

If we wanted to find student elements, we would set the match attribute to a


simple XPath expression: student.

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!

XSLT Code (Work in Progress!):


<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="student">
Found a learner!
</xsl:template>
</xsl:stylesheet>

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>

xslt- element values

Our XSLT code has been outputting "Found a learner!" for


each student element found. Let's replace that string with the student's actual
name, by using xsl:value-of to get the element's contents.

XSLT Code (Work in Progress!):


<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="class">
<html>
<body>
<xsl:apply-templates select="student"/>
</body>
</html>
</xsl:template>
<xsl:template match="student">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>

The highlighted code will return the value of the student element, which is a
student's name.

xsl:value-of uses the select attribute to choose elements. Because we are


already at the correct element, student, we use a period "." to select the current
element's data. The period is a special character to use when you want to select
the same element that you matched with template's match attribute.

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>

xslt- attribute values

xsl:value-of can also be used to retrieve the value of attributes of XML


elements. We have slightly modified our XML document to include
an id attribute, class-attr.xml.

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.

XSLT Code (Work in Progress!):


<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="class">
<html>
<body>
<xsl:apply-templates select="student"/>
</body>
</html>
</xsl:template>
<xsl:template match="student">
<p>
<xsl:value-of select="@id"/> -
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>

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>

You might also like