Solved

random xsl select

Posted on 2013-05-30
12
395 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
In the modern office, employees tend to move around the workplace a lot more freely. Conferences, collaborative groups, flexible seating and working from home require a new level of mobility. Technology has not only changed the behavior and the expe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

13 Experts available now in Live!

Get 1:1 Help Now