1

Following my question about returning a set number of random node sets using xslt 1.0 Display X distinct random node sets using XSLT 1.0

Using this code:

  <msxsl:script language="JScript" implements-prefix="my">function random() {
    return Math.random();
    }</msxsl:script>

  <xsl:template match="/">
    <output>
      <xsl:call-template name="pick-random">
        <xsl:with-param name="node-set" select="NewDataSet/Vehicle"/>
        <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(my:random() * $set-size) + 1"/>
        <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>

Which returns xml that looks a bit like this:

<output>
    <Vehicle>
       <make>something</make>
       <model>something else</model>
       <price>lots</price>
    </Vehicle>
    <Vehicle>
       <make>something</make>
       <model>something else</model>
       <price>lots</price>
    </Vehicle>
    <Vehicle>
       <make>something</make>
       <model>something else</model>
       <price>lots</price>
    </Vehicle>
    <Vehicle>
       <make>something</make>
       <model>something else</model>
       <price>lots</price>
    </Vehicle>
</output>

I'm struggling to understand how to now iterate through this returned node sets to add html styling to specific nodes.

And big thanks to Michael for the original code

Community
  • 1
  • 1
Strontium_99
  • 1,771
  • 6
  • 31
  • 52

1 Answers1

2

how to now iterate through this returned node sets to add html styling to specific nodes

Instead of:

<xsl:otherwise>
    <xsl:copy-of select="$selected"/>
</xsl:otherwise>

you could do:

<xsl:otherwise>
    <xsl:apply-templates select="$selected"/>
</xsl:otherwise>

then add templates matching the "specific nodes" you want to style. Hard to be more specific than that without seeing the expected result (at least).

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • I am in your debt once again. I couldn't put the 2 concepts together. Thanks again for your help today and yesterday. I have learnt some new stuff :) – Strontium_99 May 29 '15 at 12:30