Strontium_99
Strontium_99

Reputation: 1813

Iterating through a returned node set in XSLT 1.0

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

Upvotes: 1

Views: 273

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

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).

Upvotes: 2

Related Questions