[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

random xsl select

Posted on 2013-05-30
12
Medium Priority
?
438 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

I have purchased two new systems and both are now Universal Extensible Firmware Interface (UEFI) based. UEFI is replacing BIOS for the desktop PC. It is a Linux based firmware with enough robustness it can communicate with a website without loading …
Arrow Electronics was searching for a KVM  (Keyboard/Video/Mouse) switch that could display on one single monitor the current status of all units being tested on the rack.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 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