2013-06-19 29 views
1

我正在使用CMS製作網站,在主頁中我需要隨機獲取一些圖片和描述,我需要每次刷新頁面以獲取新節點。使用XSL隨機地從XML獲取節點

這是怎麼了調用XSL節點:

<xsl:for-each select ="TSPRoot/Blocks/Block[@ID=134]/Articles/Article[position() &lt; 4]"> 
      <div class="layout-08"> 
      <a class="title" href="detailed.aspx?id={ID}"> 
       <xsl:choose > 
       <xsl:when test ="string-length(ImageURL) > 0"> 
        <img src="_handlers/resizeimage.ashx?src={ImageURL}&amp;height=149&amp;width=206" title="{Headline}"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <img src="_frontend/ux/images/en_logo.png" width="206" height="149" title="{Headline}"/> 
       </xsl:otherwise> 
       </xsl:choose> 
      </a> 
      <div class="bg"> 
       <a href="detailed.aspx?id={ID}"><xsl:value-of select ="Headline"/></a> 
      </div> 
      </div> 
     </xsl:for-each> 

這得到最新的(3)節點,我需要我每次隨機刷新(3)節點。

+0

什麼應該在這方面隨機是什麼意思?您可以使用變量而不是常量值「4」。 –

+0

我的意思是我需要隨機3節點,每次我重新加載頁面,,, – TruthSoft

+0

我需要得到不同的照片,每次我重新加載 – TruthSoft

回答

0

我只給你一些可能的解決方案的想法。 技術上XSLT不提供方法隨機數。不過,這個問題可以以至少三種不同的方法來解決:

  1. 生成Java/C#/ PHP代碼的僞隨機數執行XSLT模板之前,並通過所產生的數量作爲XSLT參數。
  2. 使用擴展功能:http://exslt.org/random/functions/random-sequence/index.html

  3. 使用current-dateTime()(XSLT 2.0)功能,爲您的隨機數的過程的種子。通過一些字符串和數學運算,您可以將其轉換爲所需的準隨機數。我認爲,爲了您的需要,這個解決方案就足夠了。

編輯 - 廣告3.

我有這個想法做了一些實驗。我已經創建了一些示例代碼。我知道這段代碼會生成一個真正的僞隨機數的序列,但對於簡單的網頁,就像問題中的那樣,它可能就足夠了。

以下XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:rnd="http://whatever.org/random"> 
    <xsl:output indent="yes" /> 
    <xsl:function name="rnd:pseudoRandomInternal"> 
     <xsl:param name="numSeed" as="xs:decimal"/> 
     <xsl:variable name="squareSeed" select="xs:string($numSeed*$numSeed)"/> 
     <xsl:variable name="oneDivSeed" select="substring($squareSeed, 3, string-length($squareSeed) - 3)"/> 
     <xsl:variable name="cc" select="if (string-length($oneDivSeed) > 6) then 
      substring($oneDivSeed, string-length($oneDivSeed) - 6) else 
      $oneDivSeed"/> 
     <xsl:value-of select="xs:decimal($cc)"/> 
    </xsl:function> 
    <xsl:function name="rnd:pseudoRandom" as="xs:decimal*"> 
     <xsl:param name="range" as="xs:integer"/> 

     <xsl:choose> 
      <xsl:when test="$range = 1"> 
       <xsl:variable name="seed" select="substring(xs:string(current-time()), 1, 12)" as="xs:string"/> 
       <xsl:variable name="numSeed" select="xs:decimal(translate($seed, ':.+-', ''))"/> 
       <xsl:sequence select="(rnd:pseudoRandomInternal($numSeed))" /> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:variable name="subSequence" select="rnd:pseudoRandom($range - 1)"/> 
       <xsl:variable name="newValue" select="rnd:pseudoRandomInternal($subSequence[1])" as="xs:decimal*"/> 
       <xsl:sequence select="($newValue, $subSequence)"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:function> 
    <xsl:template match="/"> 
     <xsl:variable name="rr" select="rnd:pseudoRandom(10)" as="xs:decimal*"/> 

     <output> 
     <xsl:for-each select="$rr"> 
      <item> 
       <xsl:value-of select=". * 3 mod 11" /> 
      </item> 
     </xsl:for-each> 
     </output> 
    </xsl:template> 
</xsl:stylesheet> 

生成以下的輸出:

<output> 
    <item>10</item> 
    <item>9</item> 
    <item>6</item> 
    <item>9</item> 
    <item>9</item> 
    <item>10</item> 
    <item>3</item> 
    <item>5</item> 
    <item>9</item> 
    <item>4</item> 
</output> 
+0

非常感謝Lukasz Baran,我會研究這些選項,我會在解決問題後回覆。 – TruthSoft

+0

親愛的Lukasz Baran,我仍然無法解決thw問題,實際上,您提出的第一個解決方案是使用XsltSettings.EnableScript屬性創建一個符號。 關於etention函數和current-dateTime()我需要它們的幫助,current-dateTime()似乎是一個好主意,但我怎樣才能從中創建一個隨機數,以及如何使用它,因爲positoin和ID不是完全隨機的,它可能只有10個樣本,而我使用的隨機數只有10個,或者可能比隨機數少,實際上我根本不知道該怎麼做:( – TruthSoft

+0

好的,我會盡力幫助你。一個問題:你的xslt是如何被調用的?它是否被另一種語言的代碼調用? – Lukasz