遍历 XSLT 1.0 中返回的节点集
Iterating through a returned node set in XSLT 1.0
按照我关于使用 xslt 1.0 返回一组随机节点集的问题
使用此代码:
<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) < $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>
其中returnsxml那个看起来有点像这样:
<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>
我正在努力理解现在如何遍历此返回的节点集以将 html 样式添加到特定节点。
非常感谢 Michael 的原始代码
how to now iterate through this returned node sets to add html styling
to specific nodes
而不是:
<xsl:otherwise>
<xsl:copy-of select="$selected"/>
</xsl:otherwise>
你可以这样做:
<xsl:otherwise>
<xsl:apply-templates select="$selected"/>
</xsl:otherwise>
然后添加与您要设置样式的 "specific nodes" 相匹配的模板。很难比没有看到预期结果(至少)更具体。
按照我关于使用 xslt 1.0 返回一组随机节点集的问题
使用此代码:
<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) < $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>
其中returnsxml那个看起来有点像这样:
<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>
我正在努力理解现在如何遍历此返回的节点集以将 html 样式添加到特定节点。
非常感谢 Michael 的原始代码
how to now iterate through this returned node sets to add html styling to specific nodes
而不是:
<xsl:otherwise>
<xsl:copy-of select="$selected"/>
</xsl:otherwise>
你可以这样做:
<xsl:otherwise>
<xsl:apply-templates select="$selected"/>
</xsl:otherwise>
然后添加与您要设置样式的 "specific nodes" 相匹配的模板。很难比没有看到预期结果(至少)更具体。