Our website will be down for scheduled maintenance on Thursday, September 18, at 11 p.m. Pacific Time and should be back up by Midnight. We apologize for any inconvenience.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Enhanced styling with XSLT

From: Working with Data on the Web

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.

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.

Show transcript

This video is part of

Image for Working with Data on the Web
Working with Data on the Web

25 video lessons · 10144 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.

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
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?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

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:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.