XML data to HTML table - please help!

A

A. Wiebenga

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
 
M

Martin Honnen

A. Wiebenga said:
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.
 
D

David Carlisle

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>
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top