XML data to HTML table - please help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A. Wiebenga

    XML data to HTML table - please help!

    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
  • Martin Honnen

    #2
    Re: XML data to HTML table - please help!



    A. Wiebenga wrote:
    [color=blue]
    > I've got one large XML file which is roughly build up like this:
    >
    > <forms>
    > <subform>
    > <questions>
    > <question>
    > <text>ABC count</text>
    > <number=1010>[/color]
    ^^^^^^
    That doesn't look to be well-formed to me.

    --

    Martin Honnen

    Comment

    • David Carlisle

      #3
      Re: XML data to HTML table - please help!


      [color=blue]
      > 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).[/color]

      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="questio n[number &gt; 1000][contains(text,' count')]">
      <tr>
      <td><xsl:valu e-of select="substri ng-before(text,' count')"/></td>
      <td><xsl:valu e-of select="precedi ng-sibling::questi on[1]/answer/value"/></td>
      <td><xsl:valu e-of select="answer/value"/></td>
      </tr>
      </xsl:for-each>
      </table>



      Comment

      Working...