Jump to content

Rss And Xslt


Anonymous
 Share

Recommended Posts

I hope I make sense, because my English isn’t that good. But here I go: I have made an (example) xml rss feed and styled it with xslt. My rss feed have 16 feeds, now I want to limit my view on the feeds. I only want to see 5 feeds per page. And if there are more then 5 feeds I want to see a previous/next button(link) to navigate. I have searched google and some other forums and haven’t’ found anything useful. And I really want to know how I can accomplish that, so I hope you can give me some help on this?I know you can limit the nodes(feeds) with an if/when statement, but how to see the other nodes on a different page ?!Btw I just started with xml and xslt, so plz forgive me for my ignorance… :xThe code:

<?xml version="1.0" encoding="ISO-8859-1" ?><?xml-stylesheet type="text/xsl" href="testrss.xsl"?><rss version="2.0"><channel><item>  <title>W3Schools Home Page</title>  <link>http://www.w3schools.com</link>  <description>Free web building tutorials</description>  </item>  <item>	<title>RSS Tutorial</title>	<link>http://www.w3schools.com/rss</link>	<description>New RSS tutorial on W3Schools</description>  </item>  <item>	<title>XML Tutorial</title>	<link>http://www.w3schools.com/xml</link>	<description>New XML tutorial on W3Schools</description>  </item>  <item>	<title>CSS Tutorial</title>	<link>http://www.w3schools.com/css</link>	<description>New CSS tutorial on W3Schools</description>  </item>  <item>	<title>JS Tutorial</title>	<link>http://www.w3schools.com/js</link>	<description>New JavaScript tutorial on W3Schools</description>  </item>  <item>	<title>Some Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something tutorial on W3Schools</description>  </item>  <item>	<title>Some 1 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 1 tutorial on W3Schools</description>  </item>  <item>	<title>Some 2 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 2 tutorial on W3Schools</description>  </item>  <item>	<title>Some 3 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 3 tutorial on W3Schools</description>  </item>  <item>	<title>Some 4 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 4 tutorial on W3Schools</description>  </item>  <item>	<title>Some 6 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 6 tutorial on W3Schools</description>  </item>  <item>	<title>Some 7 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 7 tutorial on W3Schools</description>  </item>  <item>	<title>Some 8 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 8 tutorial on W3Schools</description>  </item>  <item>	<title>Some 9 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 9 tutorial on W3Schools</description>  </item>  <item>	<title>Some 10 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 10 tutorial on W3Schools</description>  </item>  <item>	<title>Some 11Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 11 tutorial on W3Schools</description>  </item></channel></rss>

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>	<head>		<title></title>	</head>	<body>				<xsl:for-each select="rss/channel/item">			<p>			<a>				<xsl:attribute name="href">					<xsl:value-of select="link" />				</xsl:attribute>					<xsl:value-of select="title" />			</a>						<br />			<xsl:value-of select="description" />			</p>		</xsl:for-each>		<br />		<br />		<xsl:number value="count(rss/channel/item)"/>			</body>  </html></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

I'm sorry but I tried, I couldn't get this even working with rss/channel as root. But just to make it easy for me, I removed rss/channel and made root as root node. Then your nice work worked…half…I saw only 5 items and I saw the links to switch to a different page. But when I click on those links, it doesn't go to the right page. The url change in the url bar, but it won’t change the page. It stays on the first page.I really don't know how I can fix this :)

<?xml version="1.0" encoding="ISO-8859-1" ?><?xml-stylesheet type="text/xsl" href="testrss.xsl"?><root><item>  <title>W3Schools Home Page</title>  <link>http://www.w3schools.com</link>  <description>Free web building tutorials</description>  </item>  <item>	<title>RSS Tutorial</title>	<link>http://www.w3schools.com/rss</link>	<description>New RSS tutorial on W3Schools</description>  </item>  <item>	<title>XML Tutorial</title>	<link>http://www.w3schools.com/xml</link>	<description>New XML tutorial on W3Schools</description>  </item>  <item>	<title>CSS Tutorial</title>	<link>http://www.w3schools.com/css</link>	<description>New CSS tutorial on W3Schools</description>  </item>  <item>	<title>JS Tutorial</title>	<link>http://www.w3schools.com/js</link>	<description>New JavaScript tutorial on W3Schools</description>  </item>  <item>	<title>Some Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something tutorial on W3Schools</description>  </item>  <item>	<title>Some 1 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 1 tutorial on W3Schools</description>  </item>  <item>	<title>Some 2 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 2 tutorial on W3Schools</description>  </item>  <item>	<title>Some 3 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 3 tutorial on W3Schools</description>  </item>  <item>	<title>Some 4 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 4 tutorial on W3Schools</description>  </item>  <item>	<title>Some 6 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 6 tutorial on W3Schools</description>  </item>  <item>	<title>Some 7 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 7 tutorial on W3Schools</description>  </item>  <item>	<title>Some 8 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 8 tutorial on W3Schools</description>  </item>  <item>	<title>Some 9 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 9 tutorial on W3Schools</description>  </item>  <item>	<title>Some 10 Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 10 tutorial on W3Schools</description>  </item>  <item>	<title>Some 11Tutorial</title>	<link>http://www.w3schools.com/</link>	<description>New Something 11 tutorial on W3Schools</description>  </item>  </root>

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><!--These variables are the easiest to change outside the XSLT--><xsl:param name="pageNumber" select="1" /><xsl:param name="recordsPerPage" select="5" /><!--It's neccesary to change this in order to use this XSLT.If you have multiple catalogues with different element names, you can remove this parameter and it's every occurance in this XSLT.For any other complicated XMLs, there will be a need for a lot more complex editings.--><xsl:param name="pagedElement" select="'item'" /><!--With this thing, you don't need to change anything if the pagedElement is just below the root element.--><xsl:param name="totalRecords" select="count(/*/*[name()=$pagedElement])"/><xsl:param name="totalPages" select="ceiling($totalRecords div $recordsPerPage)"/><!--This is the place to turn all or some of the controls off.You can easily do that even outside of the XSLT by setting the desired parameters to "true()".--><xsl:param name="disablePaginationNavigationControls" /><xsl:param name="disablePaginationNavigationPrevious" /><xsl:param name="disablePaginationNavigationControl" /><xsl:param name="disablePaginationNavigationNext" /><!--With theese, you can easily manipulate the URLs without many difficulties--><xsl:param name="localLinkBefore" select="'?n='"/><xsl:param name="localLinkAfter" /><!--Include a page wrapper. Optional if the rest of the page is not in this XSLT.--><xsl:template match="/"><html><head><style type="text/css">.paginationNavigation, .paginationNavigation ul {text-align: center; margin: 0 auto; padding: 0;}.paginationNavigation ul, .paginationNavigation ul li {display: inline;}.paginationNavigation span, .paginationNavigation a {padding: 10px;}</style></head><body><xsl:apply-templates /></body></html></xsl:template><!--This is the first "inside" template that gets executed. It's the place for major interface changes and wrappings.--><xsl:template match="/*"><xsl:choose><!--Change this path to match the place where your content truly is. Change the one at the for-each as well.--><xsl:when test="*[name()=$pagedElement]"><dl><xsl:variable name="startPoint" select="($pageNumber - 1) * $recordsPerPage" /><xsl:for-each select="*[name()=$pagedElement and position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]"><xsl:call-template name="pagedElement" /></xsl:for-each></dl><xsl:if test="$disablePaginationNavigationControls = false()"><xsl:call-template name="paginationNavigation"/></xsl:if></xsl:when><!--Alternative action(s) for when there are no appropriate items in the XML.--><xsl:otherwise><h1>No items to be paged.</h1></xsl:otherwise></xsl:choose></xsl:template><!-- Specifyes what to do for each paged element. Be sure to change that. --><xsl:template name="pagedElement"><dt><a href="{link}"><xsl:value-of select="title" /></a></dt><dd><xsl:value-of select="description" /></dd><!--<dd>Category: <xsl:value-of select="category" /></dd>--><dd>Number: <xsl:value-of select="position()" /></dd></xsl:template><!--This is the place for changing the paginationNavigation's wrapper and to reorder the controls.--><xsl:template name="paginationNavigation"><div class="paginationNavigation"><xsl:call-template name="previous" /><xsl:call-template name="paginationNavigationControl" /><xsl:call-template name="next" /></div></xsl:template><xsl:template name="previous"><xsl:if test="$disablePaginationNavigationPrevious = false()"><xsl:if test="$pageNumber > 1"><a href="{$localLinkBefore}{$pageNumber - 1}{$localLinkAfter}">Previous</a></xsl:if></xsl:if></xsl:template><xsl:template name="paginationNavigationControl"><xsl:if test="$disablePaginationNavigationControl = false()"><ul><xsl:for-each select="*[name()=$pagedElement and position() <= $totalPages]"><xsl:variable name="pageNumberControl" select="count(preceding-sibling::*)+1" /><li><xsl:choose><xsl:when test="not($pageNumberControl = $pageNumber)"><a href="{$localLinkBefore}{$pageNumberControl}{$localLinkAfter}"><xsl:value-of select="$pageNumberControl" /></a></xsl:when><xsl:otherwise><span><xsl:value-of select="$pageNumberControl" /></span></xsl:otherwise></xsl:choose></li></xsl:for-each></ul></xsl:if></xsl:template><xsl:template name="next"><xsl:if test="$disablePaginationNavigationNext = false()"><xsl:if test="$pageNumber < $totalPages"><a href="{$localLinkBefore}{$pageNumber + 1}{$localLinkAfter}">Next</a></xsl:if></xsl:if></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

You're using this on the client side. The default URL in localLinkBefore ("?n=") is meant to be used on the server side. If you have PHP or ASP.NET, there are also samples of them in the same topic.As for the client side, there's a need for a special function that will reexecute the transformation with new parameters. There's actually a sample of that too in this same topic, later on. Further, you'll notice you actually need to rename "pagenumber" to "pageNumber" for it to work, but still - it works.Include the mentioned page.js in the XSLT as described, then change localLinkBefore and localLinkAfter to:

<xsl:param name="localLinkBefore" select="'java script:changePage('"/><xsl:param name="localLinkAfter" select="');'"/>

(remove the space between "java" and "script" though... that space is only placed by the forum - it shouldn't be there)It should then all work.And to make it work with RSS, you only need to adjust the node in which your pagedElement is. This is done in three places:

<xsl:param name="totalRecords" select="count(/*/channel/*[name()=$pagedElement])"/>

			<!--Change this path to match the place where your content truly is. Change the one at the for-each as well.-->			<xsl:when test="channel/*[name()=$pagedElement]">

and (as mentioned in the comment above)

<xsl:for-each select="channel/*...

Edited by boen_robot
Link to comment
Share on other sites

You're using this on the client side. The default URL in localLinkBefore ("?n=") is meant to be used on the server side. If you have PHP or ASP.NET, there are also samples of them in the same topic.
I only wanted to use this with XSLT, so no php or something
As for the client side, there's a need for a special function that will reexecute the transformation with new parameters. There's actually a sample of that too in this same topic, later on. Further, you'll notice you actually need to rename "pagenumber" to "pageNumber" for it to work, but still - it works.Include the mentioned page.js in the XSLT as described, then change localLinkBefore and localLinkAfter to:
<xsl:param name="localLinkBefore" select="'java script:changePage('"/><xsl:param name="localLinkAfter" select="');'"/>

(remove the space between "java" and "script" though... that space is only placed by the forum - it shouldn't be there)

So it cant be XSLT only?! I will try it with the js file tnx!! But if it can be done with XSLT only would be nice :)
And to make it work with RSS, you only need to adjust the node in which your pagedElement is. This is done in three places:
<xsl:param name="totalRecords" select="count(/*/channel/*[name()=$pagedElement])"/>

			<!--Change this path to match the place where your content truly is. Change the one at the for-each as well.-->			<xsl:when test="channel/*[name()=$pagedElement]">

and (as mentioned in the comment above)

<xsl:for-each select="channel/*...

Tnx for the help gonna try it out ;D
Link to comment
Share on other sites

So it cant be XSLT only?! I will try it with the js file tnx!! But if it can be done with XSLT only would be nice :)
XSLT isn't a dynamic language. It takes one XML and turns it into another XML. It then stops. By the time you've generated the "n" page, XSLT is over. To move to another page, you need to reexecute the transformation, but display another page instead. Without a server side language, the only way to do so is JavaScript.If you wanted to do that with PHP and MySQL instead, you'd actually follow the same process - PHP can only generate one page at a time. The user needs to click the link to reactivate PHP, which would then generate a new page. Edited by boen_robot
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...