XML Video Tutorial 3

XML XPATH TutorialThis is a massive tutorial! It could be called a xpath tutorial / xml Template tutorial / XSL Video Tutorial. I cover all of those topics and a whole bunch more.

I’ve also spent a bunch of time looking for common questions about XML and have included those answers in this XML Video Tutorial.

If you missed part 1 of my XML Video Tutorial, check it out first. All of the code follows the video and it’s heavily commented to help you learn XML.

If you like tutorials like this, tell Google

Always feel free to share

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">
<xsl:output method="html" />

	<!-- xsl:template is used to define rules for everything in the root node -->
	<!-- The rules defined in the template can be applied to the element in
	match as well as all of its child elements or nodes -->

	<xsl:template match="/">
		<!-- apply-templates applies a template to the element matched
		and its child nodes -->
		<xsl:apply-templates select="tvshow/show" />
	<xsl:template match="show">
  		<xsl:apply-templates select="name"/> 
  		<!-- Get position versus other nodes -->
  		Node Position: <xsl:value-of select="position()" /> out of 
  		<xsl:value-of select="last()" /> nodes <br />
  		Ratings: Averaged <xsl:value-of select="viewers"/> million viewers 
  		versus the average of the rest 
  		<!-- format-number(number to format, how to show number
  		# - show when nt zero and 0 - always show a number and 
  		where you want commas and decimals-->
  		<!-- sum() sums every element that matches -->
  		<!-- div is used for division -->
  		<!-- count() returns the number of elements that match -->
  		<xsl:value-of select="format-number(sum(/tvshow/show/viewers)
  		div count(/tvshow/show), '#,##0.000')"/> million<br />
  		<!-- More functions for rounding -->
  		3.14 Rounded <xsl:value-of select="round(3.14)"/><br />
  		3.14 Ceiling <xsl:value-of select="ceiling(3.14)"/><br />
  		3.14 Floor <xsl:value-of select="floor(3.14)"/><br />
  		<!-- Extract the first name of an actor using substring-before() -->
  		<!-- I could get the last name with substring-after() -->
  		<xsl:for-each select="actors/actor">
			<xsl:value-of select="substring-before(real_name, ' ')"/> and 
		</xsl:for-each> are good actors.<br />
		Is I in Team: <xsl:value-of select="contains('Team', 'I')" /><br />
  		<br />
  		<!-- Call the template for the character -->
  		<xsl:apply-templates select="actors/actor/character"/>
	<xsl:template match="name">
		<div style="float:left">
				<xsl:attribute name="src">
						<xsl:value-of select="../poster/@href" />
		</div><br />
		<span style="color:#000; font-size: 14pt">
			<!-- . is a reference to the current node being name -->
			<!-- translate() can be used to capitalize letters -->
			<xsl:value-of select="translate(.,
			<!-- To reference a sibling of the name node use ../ -->
			was released in <xsl:value-of select="../release"/>.
			<!-- This is how you can grab other child nodes and attributes -->
			The Star <xsl:value-of select="../actors/actor/character"/> was played by 
			<xsl:value-of select="../actors/actor/character/@profession"/> 
			<xsl:text> </xsl:text><xsl:value-of select="../actors/actor/real_name"/>
		</span><br /><br />
		Release: <xsl:value-of select="../release"/><br />
		Cancelled: <xsl:value-of select="../end_date"/><br />
		Description: <xsl:value-of select="../description"/><br />
		Average Viewers: <xsl:value-of select="../viewers"/>

		<!-- You can use xsl:text to insert text. It can also be used to
		make spaces between tags which wouldn't be printed otherwise -->
		<xsl:text> </xsl:text><xsl:value-of select="../viewers/@units"/><br /><br />
		Stars<br />
		<xsl:for-each select="../actors/actor">
				<li><xsl:value-of select="character"/> played by 
				<xsl:value-of select="real_name"/></li>
		<br /><br />
	<!-- Only making a change based on a condition -->
	<xsl:template match="actors/actor/character[@profession='Detective']">
		<!-- You can define styles this way but it is better to use external files -->
		Found Detective<br />
		<span style="color:#0000ff; font-size: 14pt">
			<!-- . is a reference to the current node being name -->
			<xsl:value-of select="."/><br />
	<xsl:template match="actors/actor/character[@profession!='Detective']">
		<!-- You can define styles this way but it is better to use external files -->
		<span style="color:#fff; font-size: 14pt">
			<!-- . is a reference to the current node being name -->
			<xsl:value-of select="."/><br />

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="tvshows2.xsl" ?>

		<name>Life On Mars</name>
		<network country="US">ABC</network>
		<poster href="http://ia.media-imdb.com/images/M/MV5BMTM4MDg2NTk1OF5BMl5BanBnXkFtZTcwNzI4OTY5MQ@@._V1._SY317_CR12,0,214,317_.jpg" width="214" height="317" />
		<description>The series tells the story of New York City police detective Sam Tyler, who, after being struck by a car in 2008, regains consciousness in 1973.</description>
		<viewers units="million">7.82</viewers>
					<real_name>Jason O'Mara</real_name>
					<character profession="Detective">Sam Tyler</character>
					<real_name>Michael Imperioli</real_name>
					<character profession="Detective">Ray Carling</character>	
		<name>Life On Mars</name>
		<network country="UK">BBC</network>
		<poster href="http://ia.media-imdb.com/images/M/MV5BMTQxNzc5MjQwNF5BMl5BanBnXkFtZTcwMjU3Njc4Mg@@._V1._SY317_CR8,0,214,317_.jpg" width="214" height="317" />
		<description>The series combines elements of science fiction and police procedural, featuring a police officer who travels back in time after being involved in a road accident.</description>
		<viewers units="million">6.8</viewers>
					<real_name>John Simm</real_name>
					<character profession="Detective">Sam Tyler</character>	
					<real_name>Philip Glenister</real_name>
					<character profession="Detective">Gene Hunt</character>	
		<name>Freaks and Geeks</name>
		<network country="US">ABC</network>
		<poster href="http://ia.media-imdb.com/images/M/MV5BMTQ5OTEzODYyMl5BMl5BanBnXkFtZTcwMjcxNTcwMg@@._V1._SY317_CR24,0,214,317_.jpg" width="214" height="317" />
		<description>The show centers on a teenage girl, Lindsay Weir, and her brother, Sam, who both attend William McKinley High School during the 1980â1981 school year in the town of Chippewa, Michigan, a fictional suburb of Detroit.</description>
		<viewers units="million">6.77</viewers>
					<real_name>Linda Cardellini</real_name>
					<character profession="Student">Lindsay Weir</character>
					<real_name>John Francis Daley</real_name>
					<character profession="Student">Sam Weir</character>
		<name>Pushing Daisies</name>
		<network country="US">ABC</network>
		<poster href="http://ia.media-imdb.com/images/M/MV5BMTY3ODYxNjU1Nl5BMl5BanBnXkFtZTcwMTI0MTU1MQ@@._V1._SY317_CR0,0,214,317_.jpg" width="214" height="317" />
		<description>The series stars Lee Pace as Ned, a pie-maker with the ability to bring dead things back to life with his touch, an ability that comes with stipulations.</description>
		<viewers units="million">7.78</viewers>
					<real_name>Lee Pace</real_name>
					<character profession="Baker">Ned</character>
					<real_name>Anna Friel</real_name>
					<character profession="Baker">Charlotte Chuck Charles</character>


13 Responses to “XML Video Tutorial 3”

  1. mr.roshik says:

    Create WordPress Menu this tutorial is in your WordPress Theme Development section but there is no tutorial, when i click this tutorial, it goes to main page. please check bro.

  2. Punit says:

    hey why you stopped making java videos??

    • admin says:

      I’m not done with the Java tutorials. I allowed everyone to vote recently on the next tutorial. I received over 50 votes for an xml tutorial, so I decided to create that short tutorial. The next tutorial will be on Design Patterns using Java. The tutorial that came in 2nd place. Many more Java tutorials are coming

  3. Brian Z. says:

    Excellent tutorial series. Glad to see some tutorials on XML!!!

  4. Tram says:

    Hello there,
    I want to ask you if you already know hot to scrap emails from a website with XQuery?

    I have got some code if we can contact?


  5. ARPIT says:

    In this Video you had a problem where all your shows were being displayed in zig zag pattern , you “we’l fix that”.
    I am having the same trouble can you help me with that.

Leave a Reply

Your email address will not be published.