Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As we saw and previewed our styled RSS feed, we have fortunately articles appearing. So we want to trim that number down. To do that, we're going to need to write what's known as an XSL expression and XSL expressions use the x-path language to pass information into the XSL transformation. In this case, we want to pass into a don't display anymore than x number of articles. So we get to define that ourselves. We'll write an expression, which will look for a parameter, and then using the server transformation, we'll pass that parameter into it. So it's a two-step process. The first step is you write the expression in the XSL file and then you pass the parameter in using the server transformation.
Go ahead and open up from our exercise files, the artnews.xsl file. Now again, Dreamweaver assist you in writing these expressions. So you don't feel like you are totally on your own when you are trying to write these expressions. Dreamweaver will give you a hand with them. So with the artnews.xsl file opened, I am going to click on the XSL for each tab up top. That is the Repeat Region behavior and it's the repeat region behavior that we need to modify. We only want it to repeat X number of times, so this is a perfect place to go ahead and write our expression. So we'll select that and then using the Properties inspector at the bottom, notice that it says select RSS channel item.
There is a nice little link underneath that that says how do I create XSL for each expression. So if you are brand new to this and you want to learn more about it, you can click on that, it will launch you help file and there are some step by step instructions. So let's click on the Lightning Bolt icon just the right of our selection text field and that will bring up the XPath Expression Builder for the repeat region. Now, rather than just hand-code this, which we could if we want to, we can go ahead and hand-code the expression if we are a little more experienced in building expressions. I am going to click on the Build Filter arrow and that opens up filter and again, there is a little help box says how do I create a filter? A filter is a way of building your expression step by step. So let's click the Plus icon because we want a new expression and when I click the Plus icon, I had the Item element selected and that's pretty important. So if you didn't have the Item element selected, just click Minus, select the item and then click the Plus again because we want to filter by item.
Now, it's asking us okay, where do you want to filter this? So I am going to select where it says Title and we can pass in anything if we want to that. So what I am going to pass in is the word Position and then open parenthesis and close parenthesis. Now, I need to pass in an operator and from the Operator pulldown menu, I am going to choose less than or equal to and then for Value, I am going to pass in dollar sign, capital I, Items, capital P, per capital P, page, dollar sign, items per page, and I will hit Tab.
Notice that it's building an expression for me down at the bottom. I am seeing position is less than or equal to items per page and this is actually a pretty simple statement if you think about this. We're saying, I want you to select an item from the RSS channel where the current position of the item is less than or equal to the items per page, now the items per page at the parameter that we're passing in. So if that number items per page is higher than the current position, it won't return a value and it will stop. So it will keep adding items from our RSS feed until that number has been reached and then it will stop. So go ahead and click OK, there is our expression.
Now, I am going to save this file and we could just go back into our news.php page and pass a parameter in but it's a good idea to go ahead and test this here to make sure it's working before you go back into your server behavior. So I am going to switch over to Code view because we tested it right now. It would return an error because that parameter is not defined. So we'll need to define this. We'll define this on a line just above the XSL template tag and that's all found on line 14 and line 15. So I am just going to create a blank line right above the XSL template tab and this is what we're going to write, type in XSL and then a colon with a P, your code your cursor will jump down the param, so you want to type in param. After space, type in name equals, and then make the value equal to items per page and then the next attribute you want to pass in a default value and we'll type in select 8.
So we'll limit our initial test to 8 articles and go ahead and do a self closing tag at the end of that which is space, forward slash and then the closing angled bracket. So we will go ahead and save that and test this in our browser and I can count one, two, three, four, five, six, seven, eight articles, exactly the number that I was expecting. So let's close now Firefox, go back into Dreamweaver and now I can close my XSL file and remember this is a two-step process, so we've completed the first step.
The next and final step is to go into our news.php and modify our server behavior so that parameter is passed into our XSL transformation. So we do that by going over to our Sever Behaviors and we find the one server behavior that we have which is our XSL transformation and go ahead and double-click that. That will bring up the same dialog box we saw earlier where we created our XSL transformation. Notice that the XSL parameters instead of being grayed out is now highlighted so that we can select it. Now that's because there is an expression in the XSL transfer sheet that is expecting a parameter.
So I am going to click the Plus icon to add a value and we know that our value is going to be Items Per Page and I am just going to pass an initial value of 4. You could choose a default value if you like. If you hit Tab, notice the default value becomes 4. You can actually make the value dynamic as well. So maybe you had some other things going on and maybe you had a user interaction that said I want to display 5 articles per page or I want to display 10 articles per page. You could have that value be dynamic and then you could set a default value so that if a choice wasn't made, it would show you the default number of articles.
So I will go ahead and click OK, click OK again. I will save my page and I will test it. It's going to prompt you again if you want to update it on a testing server and it's also going to prompt you if you want any dependent files to be uploaded. We do need to upload our dependent files because we've changed our XSL file, so I am going to choose Yes, it will upload them and now in the browser, I can see my RSS feed and I am limited to 4 articles and that's a lot easier to digest than 20 or so articles that we had before. So that's exactly what we want.
So we've seen that XSL can transform content not only on the browser or client side, but also on the server side as well. We've formatted an RSS feed, we've taken raw XML data and placed it on our page and converted it to XHTML. I hope this gives you a starting point for working with your own XML data or formatting your own RSS feeds. Dreamweaver offers us a wide variety of tools and behaviors to help make styling our XML files easy and efficient.
Get unlimited access to all courses for just $25/month.Become a member
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.