Jump to content

XML SORT SELECT | LIMIT NODES | XML SEARCH


kvnmck18

Recommended Posts

I'm updating my website with all my art on it, I'm running into troubles with some stuff I'm tryign to get to work...HOPE YOU ALL CAN HELP!I am trying to make a sort for a page and it's not working... ...I am currently using a param (<xsl:param name="sort"/>)...and I'm using PHP (4)... It is being applied to a template in the xsl:sort (<xsl:sort select="$sort"/>)...but this does not seem to work because when a param passes it's like saying this:<xsl:sort select="'title'"/>...have the double quotes makes it not work.I then tried using a choose for this... like this:

<xsl:choose><xsl:when test="$sort = 'title'><xsl:sort select="title"/></xsl:when><xsl:otherwise><xsl:sort select="id"/></xsl:otherwise></xsl:choose>

...but this does not work either...hmmm... any ideas??ALSO! I am trying to limit the amount of nodes per page...I want to limit to 6 items...How should I go about this... count()?Then divide the count() of all by 6...then make the php create the amount of page#'s?...I'm not sure on that really.LAST BUT NOT LEAST:I'm coming back to my orginal thoughts on the XML search:http://w3schools.invisionzone.com/index.ph...2&hl=search...my XML is set up like an RSS feed (not used as an RSS just because I like the setup, and saying that makes you all understand how it's set up).I want the search just to search ANY WORD or ANY PART OF ANY WORD found ANY WHERE in the XML.Then post results.THANK YOU,Kevin

Link to comment
Share on other sites

For the sorting issue, you're on the right track. A choose would be a good idea. Using a variable might be possible in two conditions, but I'm not completely sure about the second.Instead of using a choose for the sort, choose a template to apply. <xsl:sort> can be a direct child of for-each OR apply-templates, making the situation quite fitting:

<xsl:param name="sort"/><xsl:template match="/"><xsl:choose><xsl:when test="$sort = 'title'><xsl:apply-templates><xsl:sort select="title"/></xsl:apply-templates></xsl:when><xsl:otherwise><xsl:apply-templates><xsl:sort select="id"/></xsl:apply-templates></xsl:otherwise></xsl:choose></xsl:template>

For limiting the number of pictures... what do you think the recordsPerPage parameter in the pagination framework is for? Exactly for that. You can simply use PHP (yes, even 4) to adjust that parameter to whatever you want.For the search, what I showed in that topic might work for fetching the appropriate nodes, however few days ago I realized there's no way (not in XSLT 1 at least) to perform a template around a part of a text node. Because of that, you can't highlight the results or anything. You can just list them.By the way, you HAVE to look into my signature. Gobby to be more precise.

Link to comment
Share on other sites

Sorting by a variable can be accomplished using a little trick:<xsl:for-each select="something/*[name() = $sort]"> <xsl:sort select="." /> <xsl:value-of .. (display data) <xsl:value-of .. (display data) </xsl:for-each>Credit to Mike Kay (again)

Link to comment
Share on other sites

I have tried the name() fnc and it didn't help that.boen-Gobby sounds badass. Great greatThe choose I think is the way to go...but for some reason it's not working. I might be doing something small that messed it up. I'm going to look at it again.And...I re-wrote the "recordsPerPage" XSLT to make it shorter and more simple and to function for my XML at hand... I think that to is also something small to fix.The search...I don't care to highlight them I just want a list. I will come back to the search after the other two problem are fixed.I'm going to work around with this...if I don't solve those two by the end of the day I'll post what I have.

Link to comment
Share on other sites

Yeah, I don't get it...when ever I though any options with xsl:sort it doesn't work. But it's not an error...it's weird. Real weird. Any other sugguestions?

Link to comment
Share on other sites

I bet it's something small. And it's just the reason why Gobby is just a dream come true :) . You know sunday's all mine :) .

Link to comment
Share on other sites

<?xml version="1.0" encoding="iso-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" omit-xml-declaration="yes"/>	<xsl:param name="columns" select="3" />	<xsl:param name="category"/>	<xsl:param name="type" />	<xsl:param name="id" />	<xsl:param name="pagedElement" select="'image'" />	<xsl:param name="recordsPerPage" select="6" />	<xsl:param name="totalitems" select="count(channel/item/image)"/>	<xsl:param name="pages_div" select="round($totalitems div $recordsPerPage)"/>	<xsl:param name="pageNumber" select="1" /><xsl:template match="channel">	<xsl:apply-templates /></xsl:template><xsl:template match="/*">	<td class="content_main">		<table cellpadding="0" cellspacing="0" class="items_table">					<xsl:call-template name="pages"/><br />					 <xsl:variable name="startPoint" select="($pageNumber - 1) * $recordsPerPage" />					<xsl:for-each select="item/*[name()=$pagedElement and position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]">						<xsl:call-template name="inside_all"/>						<xsl:if test="position() mod 2 = 0">							<tr/>						</xsl:if>					</xsl:for-each>									</table>	</td></xsl:template>	<xsl:template name="inside_all">		<td style="width:2px">		</td>		<td class="items_td_image_all">			<a href="open.php?id={id}" class="item_link_all">					<img src="{.//@thumb}" title="Click To Open {title}" class="items_listing_thumb_all" /><xsl:if test="stock = 'Sold'">					<img src="images/item_sold_small.gif" class="item_small_SOLD"/>				</xsl:if>				<div class="items_td_description_all">					<b style="font-size:14px; margin-bottom:2px;"><xsl:value-of select="title" disable-output-escaping="yes"/></b>					<br />					<xsl:for-each select=".//prices/price">						<xsl:variable name="tax" select=".*.0675"/>						<i>$<xsl:value-of select="format-number(.+$tax, '#.##')"/></i>						<xsl:value-of select="@title"/>					</xsl:for-each>				</div>			</a>		</td>		</xsl:template>		<xsl:template name="pages">		<xsl:value-of select="$pageNumber"/>/<xsl:value-of select="$pages_div"/>		<br />		Total items: <xsl:value-of select="$totalitems"/>		<br />		<xsl:if test="$pageNumber > 1">				<a href="?page={$pageNumber - 1}">					Previous				</a>		</xsl:if>		<xsl:if test="$pageNumber < $pages_div">			<a href="?page={$pageNumber + 1}">				Next			</a>		</xsl:if>		<br />		<xsl:for-each select="//*[name() = $pagedElement and position() <= $pages_div]">			<xsl:choose>				<xsl:when test="not(count(preceding-sibling::*) +1= $pageNumber)">					<a href="?page={count(preceding-sibling::*)+1}">						<xsl:value-of select="count(preceding-sibling::*) +1" />					</a>				</xsl:when>				<xsl:otherwise>					<xsl:value-of select="count(preceding-sibling::*) +1" />				</xsl:otherwise>			</xsl:choose>		</xsl:for-each>	</xsl:template></xsl:stylesheet>

It's something wrong with my xpath... the numbers keep looping and they start on 4 not one...hmmmI tried a bunch of logical paths and none worked.ANy ideas?I know it's small to fix this.

Link to comment
Share on other sites

<?xml version="1.0" encoding="iso-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" omit-xml-declaration="yes"/>	<xsl:param name="columns" select="3" />	<xsl:param name="category"/>	<xsl:param name="type" />	<xsl:param name="id" />	<xsl:param name="sim" />	<xsl:param name="sort"/>	<xsl:param name="pagedElement" select="'image'" />	<xsl:param name="recordsPerPage" select="12" />	<xsl:param name="totalitems" select="count(channel/item/image)"/>	<xsl:param name="pages_div" select="round($totalitems div $recordsPerPage)"/>	<xsl:param name="pageNumber" select="1" /><xsl:template match="channel">	<xsl:apply-templates /></xsl:template><xsl:template match="/*">	<td class="content_main">		<table cellpadding="0" cellspacing="0" class="items_table">					<xsl:call-template name="pages"/><br />					 <xsl:variable name="startPoint" select="($pageNumber - 1) * $recordsPerPage" />					<xsl:for-each select="item/image[name()=$pagedElement and position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]">												<xsl:sort select="id"  order="descending"/>						<xsl:call-template name="inside_all"/>						<xsl:if test="position() mod 2 = 0">							<tr/>						</xsl:if>					</xsl:for-each>													</table>	</td></xsl:template>	<xsl:template name="inside_all">		<td style="width:2px">		</td>		<td class="items_td_image_all">			<a href="open.php?id={id}" class="item_link_all">					<img src="{.//@thumb}" title="Click To Open {title}" class="items_listing_thumb_all" /><xsl:if test="stock = 'Sold'">					<img src="images/item_sold_small.gif" class="item_small_SOLD"/>				</xsl:if>				<div class="items_td_description_all">					<b style="font-size:14px; margin-bottom:2px;"><xsl:value-of select="title" disable-output-escaping="yes"/></b>					<br />					<xsl:for-each select=".//prices/price">						<xsl:variable name="tax" select=".*.0675"/>						<i>$<xsl:value-of select="format-number(.+$tax, '#.##')"/></i>						<xsl:value-of select="@title"/>					</xsl:for-each>				</div>			</a>		</td>		</xsl:template>		<xsl:template name="pages">		<xsl:value-of select="$pageNumber"/>/<xsl:value-of select="$pages_div"/>		<br />		Total items: <xsl:value-of select="$totalitems"/>		<br />		<xsl:if test="$pageNumber > 1">				<a href="?page={$pageNumber - 1}">					Previous				</a>		</xsl:if>		<xsl:if test="$pageNumber < $pages_div">			<a href="?page={$pageNumber + 1}">				Next			</a>		</xsl:if>		<br />		<xsl:for-each select="*[*[name() = $pagedElement and position() <= $pages_div]]">			<xsl:choose>				<xsl:when test="not(count(preceding-sibling::*) +1= $pageNumber)">					<a href="?page={count(preceding-sibling::*)+1}">						<xsl:value-of select="count(preceding-sibling::*) +1" />					</a>				</xsl:when>				<xsl:otherwise>					<xsl:value-of select="count(preceding-sibling::*) +1" />				</xsl:otherwise>			</xsl:choose>		</xsl:for-each>	</xsl:template>

It's just the path. Its bothering me so much. The "page numbers" with this are jsut 1-4 which 1-4 comes from the four sections(4 different "item"). What I want is the total count of the "image" that are in the items...but errr. God, I don't know why I can't get this... it's just a simple path problem.This one:<xsl:for-each select="item/image[name()=$pagedElement and position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]">and <xsl:for-each select="*[*[name() = $pagedElement and position() <= $pages_div]]">The XML is like an RSS (really simplified showing of the setup below)<channel><item><image></image><image></image><image></image><image></image></item><item><image></image><image></image><image></image><image></image></item><item><image></image><image></image><image></image><image></image></item></channel>

Link to comment
Share on other sites

<?xml version="1.0" encoding="iso-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" omit-xml-declaration="yes"/>	<xsl:param name="columns" select="3" />	<xsl:param name="category"/>	<xsl:param name="type" />	<xsl:param name="id" />	<xsl:param name="sim" />	<xsl:param name="sort"/>	<xsl:param name="pagedElement" select="'image'" />	<xsl:param name="recordsPerPage" select="2" />	<xsl:param name="totalitems" select="count(channel/item[title='Furniture']/image)"/>	<xsl:param name="pages_div" select="round($totalitems div $recordsPerPage)"/>	<xsl:param name="pageNumber" select="1" /><xsl:template match="channel">	<xsl:apply-templates /></xsl:template><xsl:template match="/*">	<td class="content_main">		<table cellpadding="0" cellspacing="0" class="items_table">					<xsl:call-template name="pages"/><br />					 <xsl:variable name="startPoint" select="($pageNumber - 1) * $recordsPerPage" />					<xsl:for-each select="item[title='Furniture']/image[name()=$pagedElement and position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]">												<xsl:call-template name="inside_all"/>						<xsl:if test="position() mod 2 = 0">							<tr/>						</xsl:if>					</xsl:for-each>													</table>	</td></xsl:template>	<xsl:template name="inside_all">		<td style="width:2px">		</td>		<td class="items_td_image_all">			<a href="open.php?id={id}" class="item_link_all">					<img src="{.//@thumb}" title="Click To Open {title}" class="items_listing_thumb_all" /><xsl:if test="stock = 'Sold'">					<img src="images/item_sold_small.gif" class="item_small_SOLD"/>				</xsl:if>				<div class="items_td_description_all">					<b style="font-size:14px; margin-bottom:2px;"><xsl:value-of select="title" disable-output-escaping="yes"/></b>					<br />					<xsl:for-each select=".//prices/price">						<xsl:variable name="tax" select=".*.0675"/>						<i>$<xsl:value-of select="format-number(.+$tax, '#.##')"/></i>						<xsl:value-of select="@title"/>					</xsl:for-each>				</div>			</a>		</td>		</xsl:template>		<xsl:template name="pages">		<xsl:value-of select="$pageNumber"/>/<xsl:value-of select="$pages_div"/>		<br />		Total items: <xsl:value-of select="$totalitems"/>		<br />		<xsl:if test="$pageNumber > 1">				<a href="?page={$pageNumber - 1}">					Previous				</a>		</xsl:if>		<xsl:if test="$pageNumber < $pages_div">			<a href="?page={$pageNumber + 1}">				Next			</a>		</xsl:if>		<br />		 <xsl:variable name="startNumPoint" select="($pageNumber - 1) * $totalitems" />					<xsl:for-each select="item[title='Furniture']/image[position()>=1 and position()<=$pages_div]">			<xsl:choose>				<xsl:when test="not(count(preceding-sibling::image) +1= $pageNumber)">					<a href="?page={count(preceding-sibling::image)+1}">						<xsl:value-of select="count(preceding-sibling::image) +1" />					</a>				</xsl:when>				<xsl:otherwise>					<xsl:value-of select="count(preceding-sibling::image) +1" />				</xsl:otherwise>			</xsl:choose>		</xsl:for-each>	</xsl:template></xsl:stylesheet>

This now works for the different "item" sections, now I need to get it to work for all then also the items. I still can't seem to get that to work. So it's kindof working but not at all...at least for what I want it to do.

Link to comment
Share on other sites

Err. I don't know why this is still puzzling me.I need a clear head here...help :) ?

Link to comment
Share on other sites

Err. I don't know why this is still puzzling me.I need a clear head here...help :) ?

Link to comment
Share on other sites

A plausible solution would be to simply add links to the sections that would alter just another GET variable.

item[title=$category]/image[position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]

where $category is defined earlier and adjusted by PHP just like that $pageNumber.Infact, I see you've already declared the category parameter.If you want to show all categories... that's a little bit tougher or so it seems. Maybe

*/image[position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]

or

//image[position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]

or

.//image[position()>=1+ $startPoint and position()<=$startPoint + $recordsPerPage]

I don't know.

Link to comment
Share on other sites

I know what you are saying...but I've tried that... the peculiar thing about it is...in a solution (not a REAL solution) that I came up with, everything worked BUT when the recordsperpage were equal to (for example) 2. You would expect only to see two items....but it resulted in 8. 2 from each of the 4 <item></item> sections.But the "page count" and pages divided were correct...with the right amount of pages...BUT the problem is there weren't 2 records on each page but 8...so this left like 4 times as many pages than needed. (sorry this was typed really poorly)

Link to comment
Share on other sites

Ahh... I don't know if this is THE problem, but this WAS a problem I had. Change round() to ceiling() at this line:

<xsl:param name="pages_div" select="round($totalitems div $recordsPerPage)"/>

It seems you're using a more earlier version of the pagination framework where I didn't discovered that issue. I had this problem in my own odd totalRecords div recordsPerPage combo.

Link to comment
Share on other sites

It's the position() that's messing it up.... look over the XSL...say...recordsperpage is equal to 2say...pagenumber is equal to 3position is:startpoint = 6(ending at) = 8So should just be position() <=6 >=8...but this creates a problem because it does that for each section of the <item></item>it pulls the positions <=6 >=8 from all four different <item/>s...I never noticed this, but I knew there had to be some logic to explain the chaos...That's the problem...but how to fix?

Link to comment
Share on other sites

sdklajflksdajfkl! I'm so frustrated!<xsl:for-each select="*/image[position()=1]">EQUALS 4 RESULTS! I just want 1! sadflasdrjkldasjfl

Link to comment
Share on other sites

You need to filter the item, not the image. That's just it.

item[1]/image[1]

only the first image

item/image[1]

the first image of every item.

item[1]/image

all images under the first item.

Link to comment
Share on other sites

I've tried that...that works for just section 1, or the sections (<item></items>) individually but not "as a whole" (all <item/>s in the <channel>)

Link to comment
Share on other sites

errr. I want to say I'm giving up...but I can't. I can't believe there hasn't been a solution yet.What if... I "overshoot" it. Like go to a child of the <image>So the for-each is channel/item/image/*Then for images and descriptions do ".."...but I don't think that would either.

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
×
×
  • Create New...