Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Enhanced styling with XSLT


From:

Working with Data on the Web

with Joseph Lowery

Video: Enhanced styling with XSLT

In the previous lesson, we brought some XML data into an HTML page via Javascript. Once the data's integrated it's very easily styled with CSS. That's one way to work with XML data. Another way is to use the extensible style sheet transformation language or XSLT. As the name states XSLT does more than style your data it transforms it. Covering XSLT in depth is beyond the scope of this course. If you want more details I suggest you check out Joe Marini's XML essential training here on lynda.com.
please wait ...
Watch the Online Video Course Working with Data on the Web
2h 21m Intermediate Aug 21, 2013

Viewers: in countries Watching now:

Often, designers need to convey simple data without the overhead of a relational database such as MySQL. This course introduces web technologies that make it easy for a designer or developer to store, retrieve, and display data online.

First, discover how data is collected via standard and advanced HTML5 forms. Then look at a client-friendly approach to storing that data using simple CSV files. Next, Joseph shows how to store, manage, and style data with the three big players: the granddaddy of static data, XML; the popular JavaScript-based JSON; and the recursively named YAML (YAML Ain't Markup Language), frequently used in Ruby-based projects. Plus, take a close look at basic HTML5 data options, including local storage and the flexible data attribute.

Topics include:
  • Interacting with data through HTML forms
  • Storing data as comma-separated values
  • Saving, retrieving, and displaying data as XML
  • Setting up and updating JSON data
  • Creating a YAML data file
  • Using HTML5 data storage solutions
Subjects:
Developer Web
Software:
HTML XML CSS JSON YAML
Author:
Joseph Lowery

Enhanced styling with XSLT

In the previous lesson, we brought some XML data into an HTML page via Javascript. Once the data's integrated it's very easily styled with CSS. That's one way to work with XML data. Another way is to use the extensible style sheet transformation language or XSLT. As the name states XSLT does more than style your data it transforms it. Covering XSLT in depth is beyond the scope of this course. If you want more details I suggest you check out Joe Marini's XML essential training here on lynda.com.

But in this lesson we'll use the technology to create a different kind of gallery than we did before from our existing XML data. To start we need to make a key change to the XML data file in order to make the XSLT connection. And that key change can be seen if you just open an XML data file by itself in a browser like Chrome. You'll notice at the very top of the line here it says. This XML file does not appear to have any style information associated with it. The document tree is shown below and there's our data.

What we're going to do is add in that style information. So let's head over to the code editor. Here's that same file data.xml from the chapter 30304 folder of our exercise files. Now just after the opening XML declaration, I'm going to move down a line open up a little space and insert one line of code to link to an XML stylesheet. So we start, like the XML tag, with a question mark.

And, instead of XML, it's xml-stylesheet, space, give it the type, and the type is going to be text/xsl. And then href, and that href is index.xslt. Close your quotes, and add another question mark at the end. Let's close up that extra space there just in case.

And now I'll save the file. Now to save a little time, I've already converted the basic HTML file used in the previous lesson to an XSLT document. Let me show you that in the code editor, it's index.xslt, also from Chapter 3 03_04. Now you'll note, right up top there are three XML tags. The first establishes the file as an XML document. The second notes that it's an extensible stylesheet, an XSL file.

Third, we point to the XSL template we want to work with. In XSL speak, a template targets the data that is being used from our linked XML file. Which, in this case is the root of the data, the forward slash there or in other words, all of it. Now, these last two lines, require closing elements, which are put at the bottom of the file. You'll notice they're in reverse order, like any other nested tag. So, first we close the template, on line 147.

And then we close the style sheet, on 148. There's no need to close the opening XML declaration. Okay, we're ready to insert our XSLT code. We're going to go down to our container div which, you'll recall, is around line 40. There it is on line 42, so I'll open up some space there. Tab in a bit and first what we're going to do is open up and XSL loop, using the for each property. And what are we looping through, well, all the painting nodes.

So, we'll use the select key word to point to that path. This is part of what's called, expath, another key XML technology. So the code starts angle bracket, XSL, colon. We're in the XSL namespace, four hyphen each. And then our attribute select that I mentioned. And we'll set that equal to the Xpref value which is paintings/painting. Close your quote, close the tag. Now lets go ahead and close off this tag pair here, so do a forward slash, XSL, colon for each.

Okay, that's our loop. Now we'll put the content that will appear in the loop. We don't have to use concatenation of HTML and JavaScript as we did in the previous lesson. Here we can put the HTML and XML side by side. So I'll start with a h2 tag, and I'll just go ahead and close that off. And then, the content of my h2 tag is an XML tag, xsl:value-of. The value of function gets whatever the value is for the attribute that we select.

So, we'll use select again equals title and close that tag. So, what we're doing so far is looping through the XML data, looking for each painting node, and then, within it, find the title and put that in an h2 tag. Alright, let's keep going. Next, we'll put in a paragraph tag. To add a little separation, I'm going to put in a style tag with a margin bottom of 40 pixels, straight HTML, little bit of CSS.

Oh, I see I made a little mistake here. I closed off my tag before including the attribute so let's correct that. And we'll just close off the paragraph pair here. And then within it, we'll put in our image. We have to use a slightly different syntax for each image in the for each loop in order to pull in the right image source in the XML data. So we'll put in an opening and closing image tag pair . Which is a little bit unusual from a HTML perspective, but makes perfect sense in an XML context.

Now, to specify the src, or source, we'll use the xsl attribute property. So, bracket xsl:attribute. This tells the XSLT processor to add an attribute to the tag we're in which is image. And what's that attribute's name going to be. Well, it's going to be SRC. Now whatever is within the XSL attribute tag is the value that's going to be applied.

This allows us to build up the path. So my images, which you recall from the data are just file names, no path information. Are all stored in the images folder, so I'll add that in as my path. And now we're ready to use the XSL value of property to point to the data. And we're looking for the image data. Now I realize I need to put in a closing slash here which makes me also think that I definitely need to put it in here as well.

So I went back and added the closing slash to line 44, where I have XSL value of select title. Ok, all we need to do now is to close off our XSL attribute tag. And I've already closed off my image, so that coding is done. In fact, that's all the coding we need. So now I'll save the page and let's go to the browser. Now if you looked at the XSLT file that we were just working on, all you'd see is some XML code.

However, if we browse the data.xml file, it will read in the XSLT code and then transform the page into HTML complete with our data. So check out what happens when I hit refresh, dang. As you might say. There's our regular page, and as I scroll down, you can see our big old paintings complete with titles. Everything's looking pretty good, I kind of like some of this artwork, XSLT is a really powerful technology.

Not only can you use it to transform XML data into HTML, it can also output PDF, Text, RTF, or another XML file. It's definitely worth checking out.

There are currently no FAQs about Working with Data on the Web.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Working with Data on the Web.

Return to your organization's learning portal to continue training, or close this page.


OK

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.