Solved

random xsl select

Posted on 2013-05-30
12
402 Views
Last Modified: 2013-05-30
Hi,
How to select random 5 news using xsl from xml file

thanks
0
Comment
Question by:ethar1
  • 7
  • 5
12 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39207134
That is pretty tricky

There is EXSLT supported by some XSLT1 processors, you can call in C#code or JScript code in some other processors or you can pass in a random number as a parameter

Before we can pursue this, I would like to know which XSLT processor you are using
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39207136
as you can see here
http://www.exslt.org/math/functions/random/index.html
only a few processors support math:random
0
 

Author Comment

by:ethar1
ID: 39207446
how can use : math:random()

also how can know the processor, I use Microsoft ( if that what do u means )
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39207472
Ah OK,
I mean the XSLT processor
How do you run the XSLTs, from C#?
0
 

Author Comment

by:ethar1
ID: 39208155
use asp Set oXSL = Server.CreateObject("Msxml2.DOMDocument.3.0")
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39208213
ah OK, that means you are using msxml and XSLT1
I can work with that, hang on
0
 

Author Comment

by:ethar1
ID: 39208247
is there is a better option?
0
 

Author Comment

by:ethar1
ID: 39208248
is there is a better option?  XSLT2 or something?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39208254
XSLT2 has a number of benefits, but integration in ASP is still a bit tricky
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39208432
This one will work only for msxml... and it is a bit crafty :-)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:atom="http://www.w3.org/2005/Atom"
     exclude-result-prefixes="atom math msxsl"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:math="http://exslt.org/math"
     extension-element-prefixes="math msxsl"
     version="1.0">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <msxsl:script language="JavaScript" implements-prefix="math">
        <![CDATA[
        function random()
        {
        return Math.random();
        }
        ]]>
    </msxsl:script>
    
    <xsl:variable name="item-count" select="count(//item)"></xsl:variable>
    
    <xsl:variable name="random-sequence">
        <random-sequence item-count="{$item-count}">
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
            <item><xsl:value-of select="ceiling(math:random() * ($item-count - 1))"/></item>
        </random-sequence>
    </xsl:variable>
    
    <xsl:template match="/rss">
        <xsl:variable name="rss" select="."/>
        <html>
            <head>
                <!--xsl:copy-of select="msxsl:node-set($random-sequence)"/-->
            </head>
            <body>
                <h1><a href="{channel/link}"><xsl:value-of select="channel/title"/></a></h1>
                <xsl:for-each select="msxsl:node-set($random-sequence)//item[not(preceding-sibling::item = .)][position() &lt;= 5]">
                    <xsl:variable name="item-no" select="."></xsl:variable>
                    <xsl:apply-templates select="($rss/channel/item)[position() = $item-no]"/>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
    
     
    <xsl:template match="item">
        <h3><a href="{link}"><xsl:value-of select="title"/></a></h3>
        <p><xsl:value-of select="pubDate"/></p>
        <p><xsl:apply-templates select="description"/></p>
        <xsl:if test="enclosure[contains(@type, 'image')]">
            <img src="{enclosure/url}"/>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="description">
        <xsl:variable name="pre-link-str">
            <xsl:choose>
                <xsl:when test="contains(., '&lt;a')">
                    <xsl:value-of select="substring-before(., '&lt;a')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:call-template name="remove-br">
            <xsl:with-param name="str" select="$pre-link-str"/>
        </xsl:call-template>
        
    </xsl:template>
    
    <xsl:template name="remove-br">
        <xsl:param name="str"/>
        <xsl:choose>
            <xsl:when test="contains($str, '&lt;br')">
                <xsl:value-of select="substring-before($str, '&lt;br')"/>
                <xsl:variable name="remainder-str" select="substring-after($str, '&lt;br')"/>
                <xsl:choose>
                    <xsl:when test="starts-with(normalize-space($remainder-str), '/&gt;')">
                        <xsl:call-template name="remove-br">
                            <xsl:with-param name="str" select="substring-after($remainder-str, '/&gt;')"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:call-template name="remove-br">
                            <xsl:with-param name="str" select="substring-after(substring-after($remainder-str, '&lt;/'), '&gt;')"/>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$str"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:ethar1
ID: 39209319
thnx , work like charm
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39209336
welcome
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
UPS Power design 11 72
Dell server: Fans race daily 9 60
iPhone 6S - ios10 - Text Messaging sounds 2 73
Convert MS Word document to a PDF file 9 78
Hello It is a very old trick to use a ram disk in order to boost PC performances, in the past, when in home environments the fastest common devices were floppy disks a part of the very small ram memory available was used to create a virtual hard …
More and more people are using the enhanced small form-factor pluggable (SFP+) tranceivers, and speed is of utmost importance. Testing of speeds are critical to ensure that the devices will meet the speed requirements. There are some testing challen…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now