XML data to HTML table - please help!

Discussion in 'XML' started by A. Wiebenga, Jan 24, 2005.

  1. A. Wiebenga

    A. Wiebenga Guest

    Hi all!

    I am currently involved in a project in which I am supposed to write a
    XSLT-transformation sheet for some XML data.

    I will outline the situation first:
    I've got one large XML file which is roughly build up like this:

    <forms>
    <subform>
    <questions>
    <question>
    <text>ABC count</text>
    <number=1010>
    <answer>
    <value>1</value>
    </answer>
    </question>
    <question>
    <text>ABC score</text>
    <number=1020>
    <answer>
    <value>10</value>
    </answer>
    </question>
    <question>
    <text>DEF score</text>
    <number=1030>
    <answer>
    <value>3</value>
    </answer>
    </question>
    <question>
    <text>DEF count</text>
    <number=1040>
    <answer>
    <value>2</value>
    </answer>
    </question>
    </questions>
    </subform>
    </forms>
    ....etc...

    I want this data to be in a table like this:
    TYPE SCORE COUNT
    ABC 10 1
    DEF ...etc.

    The problem being that not all of the question/answer tags are needed.
    So I need to loop through all question-nodes (roughly 100)and extract
    only a few elements for the table. The needed tags are recognisable
    because there "number" is above 1000. Furthermore, there are only 3
    seperate values per type (I've used only 2 for my example).

    I've started as follows:
    First I loop through all the question-nodes via a <xsl:for-each> loop.
    I then test each question-node for a "number"-tag that exceeds 1000 (I
    only need these nodes for my table).

    These two steps bring me at the point where I can fill my table. It's
    at this point that I am stuck. I can't figure out how I am supposed to
    know when to start a new row. To my knowledge, it's not possible to
    keep track of a counter in XSLT (if this were possible, I could just
    count the number of columns I'd have filled). I've tried using
    <xsl:when> statements to start a new row using hardcoded borders (for
    example: I know the row ends after a ABC->COUNT node), but this is not
    possible because of the nested structure of XML.

    I don't know what to do now, so please help..

    Thanks in advance,

    Alke Wiebenga
    A. Wiebenga, Jan 24, 2005
    #1
    1. Advertising

  2. A. Wiebenga wrote:

    > I've got one large XML file which is roughly build up like this:
    >
    > <forms>
    > <subform>
    > <questions>
    > <question>
    > <text>ABC count</text>
    > <number=1010>

    ^^^^^^
    That doesn't look to be well-formed to me.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 24, 2005
    #2
    1. Advertising


  3. > I've started as follows:
    > First I loop through all the question-nodes via a <xsl:for-each> loop.
    > I then test each question-node for a "number"-tag that exceeds 1000 (I
    > only need these nodes for my table).


    No need to select everything and then throw most away, just select the
    things you need, something like this if I understood your structure
    correctly.


    <table>
    <tr>
    <td>type</td>
    <td>score</td>
    <td>count</td>
    </tr>
    <xsl:for-each select="question[number &gt; 1000][contains(text,'count')]">
    <tr>
    <td><xsl:value-of select="substring-before(text,' count')"/></td>
    <td><xsl:value-of select="preceding-sibling::question[1]/answer/value"/></td>
    <td><xsl:value-of select="answer/value"/></td>
    </tr>
    </xsl:for-each>
    </table>
    David Carlisle, Jan 24, 2005
    #3
    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. Rio
    Replies:
    4
    Views:
    1,172
  2. KK
    Replies:
    2
    Views:
    515
    Big Brian
    Oct 14, 2003
  3. Replies:
    3
    Views:
    2,170
    Stephen Slyman
    Jun 9, 2006
  4. Replies:
    9
    Views:
    485
    Bruno Desthuilliers
    Dec 28, 2007
  5. Renie83
    Replies:
    1
    Views:
    296
    Ray at
    Jul 9, 2003
Loading...

Share This Page