Display X distinct random node sets using XSLT 1.0

2019-05-31 16:32发布

问题:

I've got a simple xsl code to display some dynamically xml.

<xsl:template match="/">
  <xsl:for-each select="NewDataSet/Vehicle">
    <div class="item"> 
        <xsl:value-of select="ManufacturerName" /><br />
        <xsl:value-of select="Model" /><br />
        <xsl:value-of select="Colour" /><br />
        £<xsl:value-of select='format-number(Price, "###,###,##0.")' /> 
    </div>
  </xsl:for-each>
</xsl:template>

What I'd like to be able to do is display X number of random distinct node sets instead of all of them. Is this possible using xslt 1.0?

Thanks.

回答1:

Here is an example that shows how to pick N random nodes out of a given node-set.

You must have a method to generate a random number between 0 and 1 (not including 1) to make this work. In this example, the EXSLT math:random() extension function is used for this purpose. If you are using the MSXML processor, replace this with an extension function as shown here: https://stackoverflow.com/a/6607235/3016153

XML

<list>
    <item id="1">001</item>
    <item id="2">002</item>
    <item id="3">003</item>
    <item id="4">004</item>
    <item id="5">005</item>
    <item id="6">006</item>
    <item id="7">007</item>
    <item id="8">008</item>
    <item id="9">009</item>
    <item id="10">010</item>
    <item id="11">011</item>
    <item id="12">012</item>
</list>

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="http://exslt.org/math"
extension-element-prefixes="math">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/list">
    <output>
        <xsl:call-template name="pick-random">
            <xsl:with-param name="node-set" select="item"/>
            <xsl:with-param name="quota" select="5"/>
        </xsl:call-template>
    </output>
</xsl:template>

<xsl:template name="pick-random">
    <xsl:param name="node-set"/>
    <xsl:param name="quota"/>
    <xsl:param name="selected" select="dummy-node"/>    
    <xsl:choose>
        <xsl:when test="count($selected) &lt; $quota and $node-set">
            <xsl:variable name="set-size" select="count($node-set)"/>    
            <xsl:variable name="rand" select="floor(math:random() * $set-size) + 1"/>       
            <!-- recursive call -->
            <xsl:call-template name="pick-random">
                <xsl:with-param name="node-set" select="$node-set[not(position()=$rand)]"/>
                <xsl:with-param name="quota" select="$quota"/>
                <xsl:with-param name="selected" select="$selected | $node-set[$rand]"/>         
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="$selected"/>
        </xsl:otherwise>
     </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Result (of an example run)

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <item id="1">001</item>
   <item id="3">003</item>
   <item id="8">008</item>
   <item id="10">010</item>
   <item id="12">012</item>
</output>

Note: This method preserves the internal order of the selected items.