Solved

random xsl select

Posted on 2013-05-30
12
398 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IPhone 6S AC Adapter 8 81
Is pure sine wave power necessary for laptop charging? 11 326
power cord wear? 12 68
Fan for HP TouchSmart 600-1265qd CTO Desktop PC 7 74
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

22 Experts available now in Live!

Get 1:1 Help Now