Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL only show 10 loops in the for-each

Tags:

xslt

My XML has 100 AgentSales nodes I only want to show the first 10 so far I have

<xsl:for-each select="NewDataSet/AgentSales">
    <tr>
        <xsl:if test="(position() mod 2 = 1)">
            <xsl:attribute name="bgcolor">#cccccc</xsl:attribute>
        </xsl:if>
        <td>
            <span style="font:20px arial; font-weight:bold;">
                <xsl:value-of select="AgentName"/>
            </span>
        </td>
        <td>
            <span style="font:20px arial; font-weight:bold;">
                <xsl:value-of select="State"/>
            </span>
        </td>
        <td>
            <span style="font:20px arial; font-weight:bold;">
                <xsl:value-of select="time"/>
            </span>
        </td>
    </tr>
</xsl:for-each>

New to the site but when I use the code brackets not all of my code shows? at least not in the preview below.

like image 360
Denoteone Avatar asked Dec 10 '10 03:12

Denoteone


People also ask

How do you do a for loop in XSLT?

You create an XSLT loop with the <xsl:for-each> tag. The value of the select attribute in this tag is an XPath expression that allows you to specify the data element to loop through. XPath expressions are constructed like file paths in an operating system, the forward slash (/) selects subdirectories.

What is xsl for-each select?

The <xsl:for-each> element selects a set of nodes and processes each of them in the same way. It is often used to iterate through a set of nodes or to change the current node. If one or more <xsl:sort> elements appear as the children of this element, sorting occurs before processing.

What is Number () in XSLT?

Specifies the format pattern. Here are some of the characters used in the formatting pattern: 0 (Digit) # (Digit, zero shows as absent)

What is the use of for-each in XSLT?

The <xsl:for-each> element establishes the context for iteration. The XSLT transformation instructions within this loop are to be applied to the selected nodes.


2 Answers

Use:

<xsl:for-each select="NewDataSet/AgentSales[not(position() >10)]">
  <!-- Process each node from the node-list -->
</xsl:for-each>

Even better:

<xsl:apply-templates select="NewDataSet/AgentSales[not(position() >10)]"/>
like image 110
Dimitre Novatchev Avatar answered Oct 11 '22 15:10

Dimitre Novatchev


Try something like:

<xsl:for-each select="NewDataSet/AgentSales">
    <xsl:if test="position() &lt;= 10">
        ...
    </xsl:if>
</xsl:for-each>
like image 2
lesscode Avatar answered Oct 11 '22 16:10

lesscode