XSLT Sequential Numbering

Discussion in 'XML' started by Liam, Jul 17, 2007.

  1. Liam

    Liam Guest

    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
     
    Liam, Jul 17, 2007
    #1
    1. Advertising

  2. Liam

    Pavel Lepin Guest

    Liam <> wrote in
    <>:
    > 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.

    --
    ....the pleasure of obedience is pretty thin compared with
    the pleasure of hearing a rotten tomato hit someone in the
    rear end. -- Garrison Keillor
     
    Pavel Lepin, Jul 17, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. buran

    numbering rows in datagrid

    buran, Jul 7, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    471
    Saravana
    Jul 7, 2003
  2. James Merdan

    XSLT numbering by attribute

    James Merdan, Sep 24, 2003, in forum: XML
    Replies:
    1
    Views:
    414
    Dimitre Novatchev
    Sep 24, 2003
  3. pstachy

    sort and numbering - xslt

    pstachy, Nov 26, 2006, in forum: XML
    Replies:
    7
    Views:
    568
  4. Michael Doubez
    Replies:
    1
    Views:
    533
    Peter Flynn
    Jan 27, 2007
  5. tuka
    Replies:
    3
    Views:
    695
    Joe Kesselman
    Jun 22, 2007
Loading...

Share This Page