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.
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.
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.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.