Solved

random xsl select

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

thanks
0
Comment
Question by:ethar1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Memory (not to be confused with Hard Drive Space) can greatly increase the performance of a computer. Each version of Window has greater demands on memory. My personal recommendation to get decent performance out of a machine is as follows: Windows…
This article applies to This article will be of use if you are the owner / supporter of any computer that comes with a discrete graphics card from nVidia / ATI Radeon (Now owned by AMD) or any embedded graphics solution and need to upgrade the gr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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