XML Video Tutorial 2

XSL Video TutorialIn part 1 of my XML Video Tutorial, I covered the basics of creating an XML file in Eclipse. In this tutorial, I teach you how to use eXtensible Style Language (XSL).

I can’t even briefly cover everything in this tutorial. Suffice to say, I show you how to do just about anything you’d ever want to do with XSL. At least I covered everything I could fit in a 15 minute video.

Use the code below to help you better understand XSL.

If you like videos like this, tell Google [googleplusone]

I always appreciate sharing

Code From the Video

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

	<!-- XSLT: eXtensible Style Language Used for styling XML -->
	<!-- This sets the output type (Not Needed for HTML) -->
	<xsl:output method="html" />
	<!-- xsl:template : States that we want to match the whole document -->
	<!-- All of the rules that follow will apply to the root node -->
	<xsl:template match="/">
			<head><title>TV Shows</title></head>
				<!-- Return the XSLT Version used -->
				Version: <xsl:value-of select="system-property('xsl:version')" /><br />
				<!-- Return the XSLT Vendor used -->
				<!-- libxml2 : Toolkit developed for the Gnome project -->
				Vendor: <xsl:value-of select="system-property('xsl:vendor')" /><br />
				Vendor URL: <xsl:value-of select="system-property('xsl:vendor-url')" /><br />
				<!-- Cycles through each node in the XML file -->
				<xsl:for-each select="tvshow/show">
					<!-- generate-id() returns a unique node identifier -->
					<a href="#{generate-id(name)}">
					<!-- Get the value of name for the node -->
					<xsl:value-of select="name" /></a><br />
				<br />
				<!-- Cycles through each node in the XML file -->
				<xsl:for-each select="tvshow/show">
					<!-- xsl:sort sorts based on your rules 
						select: node to base sort on
						order: ascending (default) or descending
						data-type: text or number	-->
					<xsl:sort select="name" order="ascending" data-type="text" />
					<!-- Print show name with a link to the list above -->
					<h3><a name="{generate-id(name)}">
					<xsl:value-of select="name" /></a></h3>
					<!-- Here I create an img tag and add attributes to it -->
						<!-- Select the attribute to change -->
						<xsl:attribute name="src"> 
						<!-- Set the value for the attribute -->
						<!-- You grab the attribute of a node with @ -->
						<xsl:value-of select="poster/@href" /> 
					</img><br />
					<!-- Returns the current node information -->
					<xsl:value-of select="current()"/>
					<p>The show <xsl:value-of select="name"/> was released in
					<xsl:value-of select="release"/> by 
					<xsl:value-of select="network"/>. It had an average 
					viewership of <xsl:value-of select="viewers"/> million people.
					It was cancelled in <xsl:value-of select="end_date"/>
					</p><br />
				<!-- Output Information in a table -->
				<table border="2">
				<xsl:for-each select="tvshow/show">
				<!-- Checks if the attribute of network is not equal to UK -->
				<!-- This will only allow non-UK shows on the list -->
				<!-- You can use: = != > &lt; -->
				<!-- <xsl:if test="network[@country='UK']"> -->
				<!-- Shows results when release < 2006 -->
				<xsl:if test="release &lt; 2006">
					<td><xsl:value-of select="name"/></td>
					<td><xsl:value-of select="network"/></td>
					<td><xsl:value-of select="viewers"/></td>
				<!-- xsl:choose is used to cover many conditions -->
					<!-- xsl:when defines under what condition to execute -->
					<xsl:when test="release > 2006">
						<tr bgcolor="yellow">
							<td><xsl:value-of select="name"/></td>
							<td><xsl:value-of select="network"/></td>
							<td><xsl:value-of select="viewers"/></td>
					<xsl:when test="release = 2006">
						<tr bgcolor="orange">
							<td><xsl:value-of select="name"/></td>
							<td><xsl:value-of select="network"/></td>
							<td><xsl:value-of select="viewers"/></td>
					<!-- If xsl:if was removed this would be the default action for xsl:choose  -->
						<tr bgcolor="pink">
							<td><xsl:value-of select="name"/></td>
							<td><xsl:value-of select="network"/></td>
							<td><xsl:value-of select="viewers"/></td>


35 Responses to “XML Video Tutorial 2”

  1. mr.roshik says:

    Thanks for sharing 🙂 this tutorial

  2. techfreak says:

    It is a very big big big thank you in the world for sharing your knowledge with all of us. Wish you all the best in whatever you do.

  3. gripmaster says:

    Thanks for your videos, they are really cool!
    I tried to run this example by copy/pasting your code. Strange thing is the .xml page gets properly rendered in Firefox, but in none of Safari, Chrome or the internal Eclipse editor on my machine. Do you have any idea why this would happen?

    • admin says:

      Yes, Chrome and other browsers don’t allow you to load local XML files. If you put them on a server on the internet they will work for you though

    • admin says:

      Some browsers don’t properly render local files. They will work if you upload the files to a server on the web though. Of course the way I demonstrate and Firefox work for development locally. I hope that makes sense

      • gripmaster says:

        Thanks for your quick reply Derek, I imagined that Chrome wouldn’t allow this for security reasons, I more surprised that for me, it works for Firefox and not with Eclipse’s built-in browser as it does for you.

  4. Punit says:

    Hi Darek, In this video you added xsl file in eclipse, but when i am trying to add there is no xsl file in my eclipse, can you tell me why?
    i am using eclipse juno, and it has xml perspective.
    it only has xml file, dtd file and xml schema file.

  5. Punit says:

    Darek i have a question, yesterday i was thinking to do some project to get some experience, than i thought i should contribute in some open source projects, like: jenkins and all.
    need your suggestions and help and sources for how to get starte?
    Thank you

    • admin says:

      I’ve only worked to help one open source project directly and that was WordPress. Many years ago (before I made my website) I made a bunch of themes and plugins for WordPress. I personally didn’t enjoy the experience. I made many themes with completely different layouts. I also made a ton of plugins that solved many simple issues as well as a auto posting plugin.

      If you want to receive hateful emails everyday while working for free, developing free wordpress plugins is the way to go. Most users don’t read the guides you make. So, if you require them to do anything they will complain. Maybe other people have had different experiences and I’d love to hear some positive stories, but mine wasn’t very much fun.

      Just my 2 cents 🙂

      • punit says:

        well, so far my aim is to get experience and add many entries to my resume 😉 to get a good job.
        and would like to get this experience also.
        can you please tell me how to get started, i mean i need to create one account on github and need to configure that and need to import the code into eclipse, isn’t it?
        but not sure how to do that?

        • admin says:

          You’ll have to contact someone on the project by email. Tell them you’d like to contribute and they will then fit you into the position that would be best for you. These groups work in collaboration and communicate often on the common goals of the project. They’ll be happy to welcome you in in most situations

  6. Rob says:

    Can you help me to have a space

  7. Rob says:

    Thanks I got my answer in the next tutorial, you covered a lot of thing in less time ,great!!

  8. Rob says:

    Thanks,I got my answer in the next tutorial. You covered a lot of thing in less time!!!:)

  9. Andy says:

    Thanks for the tutorials. One bit of feedback I would give, is the attribute name choices.

    As you have used ‘name’ for the title, when you create your links it might not be overly clear what you can and cannot change. For example if someone was using ‘title’, they would use “href=”#{generate-id(title)}”>” to generate the link, but then the link would be ““.

    Someone might think you change all instances of name, but name is actually required there, so if you were using title it would not be “” just because you have “

    That might sound daft but hey, thanks again.

  10. Rasme says:

    Hi sir, thanks for the tutorial. I use eclipse helios… xml, xml dtd and xml schema in it but xslt aint in it…what eclipse ide do you use

  11. Lakshma says:

    Hello sir, i have a problem running this example. Am using Eclipse Kepler version. When i tried to open the .xml in web browser am getting “Life on Mars 2008 2009 ABC 7.82 Life on Mars 2006 2007 BBC 6.8 Freeks and Geeks 1999 2000 ABC 6.7 ” only this as output. What you think may be the problem. Please help me fix this problem sir.

Leave a Reply

Your email address will not be published.