1 users online. Create an account or sign in to join them.Users
How to select a random node from external XML
This is an open discussion with 7 replies, filed under XSLT.
Search
Try Pulling images randomly from an existing datasource. An oldie but a goodie.
Hi Nick. Thanks for your response and sorry for the delay replying, I've been on holiday.
I have tried all of these techniques and fun into problems with all of them - probably due to my poor understanding of the structure of XSLT.
I've focused on the latter EXSLT solution but don't really understand how it should be structured within my site. So here are a few details:
The XML looks like this:
<site-featured status="fresh" creation="2011-09-19T10:05:18+01:00"> <item> <Id>a0AD000000KGyQ6MAL</Id> <Name>0</Name> <PurpleLocation__c>thailand</PurpleLocation__c> <pb__ItemName__c>Pearl of Naithon</pb__ItemName__c> <Purchase_Price__c>13000000</Purchase_Price__c> <CurrencyIsoCode>THB</CurrencyIsoCode> </item> <item> <Id>a0AD000000KH8hVMAT</Id> <Name>0</Name> <PurpleLocation__c>turkey</PurpleLocation__c> <pb__ItemName__c>Terrace Life – Gocek</pb__ItemName__c> <Purchase_Price__c>99500</Purchase_Price__c> <CurrencyIsoCode>GBP</CurrencyIsoCode> </item> </site-featured>
The XSLT template looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:math="http://exslt.org/math"
extension-element-prefixes="exsl" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="rand">
<xsl:variable name="entries">
<xsl:for-each select="/data/site-featured/item">
<entry-container>
<xsl:attribute name="random"><xsl:value-of select="floor(math:random() * 10)" /></xsl:attribute>
<xsl:copy-of select="." />
</entry-container>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="exsl:node-set($entries)/entry-container">
<xsl:sort select="@random" order="ascending" data-type="number" />
<xsl:call-template name="featured-sidebar" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The template "featured-sidebar" which shows the information for the selected entry looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="featured-sidebar">
<div class="featured">
<h4>Featured Developments</h4>
<h5><xsl:value-of select="pb__ItemName__c" />, <span><xsl:value-of select="PurpleLocation__c" /></span></h5>
<p>From only <xsl:call-template name="currency"><xsl:with-param name="string" select="CurrencyIsoCode"/></xsl:call-template><xsl:value-of select="Purchase_Price__c" /></p>
<a class="btn" href="{$root}/properties-for-sale/{PurpleLocation__c}/{Id}/">More info</a>
</div>
</xsl:template>
</xsl:stylesheet>
I am calling the template from my master.xsl template using:
<xsl:call-template name="rand" />
The result is that I see one instance of the featured-sidebar template but that no data shows up in the template. I have not used EXSLT before so don't know if I need to declare that I am using it in the master.xsl template or only in the specific template that uses the function.
I suspect I have strung things together incorrectly. Am I close?
Thanks. S
Sorry Stuart, I completely forgot about this thread. I can't see anything wrong with your code that would make this happen, so I presume you resolved this in time.
I came across this thread again because I've just needed to implement random sorting in XSLT. There's actually a much simpler way of achieving this without building your XML into a new nodeset and adding a random attribute. Simply sort using the random function!
Include the common and math namespaces as you have done, then your apply templates:
<xsl:apply-templates select="stuff/entry">
<xsl:sort select="math:random()" data-type="number" order="ascending" />
</xsl:apply-templates>
I didn't think this would work, but it does. Hope that saves a few minutes next time!
Thanks Nick. It actually remains unsolved. I'll try this. It looks like the exact answer I was looking for.
Thanks Nick. This works a treat. I take my hat off once more :-)
Rad.
Thanks, Nick!
Create an account or sign in to comment.
I am pulling in data from an external XML source that can't sort by random.
What I'd like to do is show a single random entry from a list of entries on each page.
Can anyone think of an XSL solution to this please?
Thanks