xsl:number newb question

G

Guiding5

Hi group!

Basic question about counting specific nodes in the range, here the
example:

XSL:

<xsl:stylesheet version="1.0">
<xsl:template match="/">

Red cars:
<xsl:for-each select="car">
<xsl:if test="color = 'red'">
<xsl:number select="position()"/>.
<xsl:value-of select="manufacturer"/> ( <xsl:value-of
select="model"/> )
</xsl:if>
</xsl:for-each>

Blue cars:
<xsl:for-each select="car">
<xsl:if test="color = 'blue'">
<xsl:number select="position()"/>.
<xsl:value-of select="manufacturer"/> ( <xsl:value-of
select="model"/> )
</xsl:if>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

I want to get XSLT result that will look like:
Red cars:
1. Ford (67)
2. AM (78)

Blue cars:
1. Fiat (92)
2. Toyota (06)
3. Ford (05)

But instead of it I get numbering of actualy nodes positions in
original XML.
 
S

steve_marjoribanks

I don't quite get what you're asking here, could you elaborate? What
number do you want if its not the node number?
 
G

Guiding5

Well, with given stylesheet I will get something like this (instead of
wanted):

1. Ford (67)
4. AM (78)

Blue cars:
2. Fiat (92)
3. Toyota (06)
5. Ford (05)

Counting will be according the real positions of elements in input XML.
The question is, how do I count elements in resulting XSLT ? How do I
get counting related to if condition ?
 
G

Guiding5

This actually did the job!

Thanks alot, it works, even if I still dont understand why :)
 
M

Martin Honnen

Guiding5 said:
it works, even if I still dont understand why :)

position() gives the position in the current node list, you had all car
elements in that list while my approach made sure the current node list
had only the red (or the blue) cars when position() is evaluated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top