XSLT Sequential Numbering

L

Liam

I am totally new to XML and XSLT, so I I have an XML file of:

<log>
<logvalue code="500" description="desc" >
<logvalue code="510" description="desc" >
<logvalue code="504" description="desc" >
<logvalue code="1" description="desc" >
<logvalue code="503" description="desc" >
<log>

I want to process it using XSLT transform. I want to generate a table
as HTML output with the first column as a sequential value of rows. I
am using :-

<xsl:if test='count(//log/logvalue [@code>=500]) &gt;
0'>
<xsl:if test='count(//log/logvalue
[@code&lt;=510]) &gt; 0'>
<table border="1">
<tr>
<th>Identifier</th>
<th>Code</th>
<th>Detail</th>
</tr>
<xsl:for-each select="//log/logvalue ">
<xsl:if test="@code>=500">
<xsl:if test="@code&lt;=510">
<tr>
<td style="width:10%">
<xsl:number/>
</td>

<td style="width:30%">
<xsl:value-of
select=@code/>
</td>

<td style="width:60%">
<<xsl:value-of
select="@description"/>
</td>
</tr>
</xsl:if>
</xsl:if>
</xsl:for-each>
</table>
</xsl:if>
</xsl:if>

Now the Identifier column should display a sequential value for each
row that is printed but when I use <xsl:number/> it is the position in
the list rather than the incremental value as it is printed.

Can anyone help here?

Thanks,Liam
 
P

Pavel Lepin

Liam said:
I am totally new to XML and XSLT, so I I have an XML file
of:

I would recommend first reading some sort of tutorial then.
<log>
<logvalue code="500" description="desc" >
<logvalue code="510" description="desc" >
<logvalue code="504" description="desc" >
<logvalue code="1" description="desc" >
<logvalue code="503" description="desc" >
<log>

Not well-formed.

[transformation snipped]

Not a complete transformation, not well-formed and
horrendously indented with no respect for the 78 chars
rule. You sure you want help?
Now the Identifier column should display a sequential
value for each row that is printed but when I use
<xsl:number/> it is the position in the list rather than
the incremental value as it is printed.

The typical solution would be something along the lines of:

<xsl:template match="log">
<result>
<xsl:apply-templates
select=
"
logvalue[@code >= 500 and @code &lt;= 510]
">
<xsl:sort select="@code"/>
</xsl:apply-templates>
</result>
</xsl:template>
<xsl:template match="logvalue">
<row>
<cell><xsl:value-of select="position()"/></cell>
<cell><xsl:value-of select="@code"/></cell>
<cell><xsl:value-of select="@description"/></cell>
</row>
</xsl:template>

Sometimes this is not feasible, and in that case the best
you can do is define a named template to calculate the row
position, using something like (untested):

count
(
../logvalue
[@code >= 500 and @code &lt;= 510]
[@code &lt; $current/@code]
)

Recursive processing might be an option as well, but this
path is fraught with peril.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top