-1

Let's say I have this XML file

<section name="AAA">
    <Item1>FALSE</Item1>
    <Item2>FALSE</Item2>
    <Item3>FALSE</Item3>
</section>
<section name="BBB">
    <Item1>FALSE</Item1>
    <Item2>FALSE</Item2>
    <Item3>FALSE</Item3>
</section>

Now I want to create an xsl file that will display the data in a table looks like that

           AAA          BBB
Item1      FALSE        FALSE
Item2      FALSE        FALSE
Item3      FALSE        FALSE

i tried several syntax but none gave me what I want Can I get help here or example ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Epligam
  • 741
  • 2
  • 14
  • 36

2 Answers2

1

The following solution will work for any number of sections and any number of items, granted every section has the same number of items:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <table>
            <thead>
                <tr>
                    <th></th>
                    <xsl:for-each select="//section">
                        <th>
                            <xsl:value-of select="@name" />
                        </th>
                    </xsl:for-each>
                </tr>
            </thead>
            <tbody>
                <xsl:for-each select="//section[1]/*">
                    <tr>
                        <td>
                            <xsl:value-of select="name()" />
                        </td>
                        <xsl:variable name="row" select="position()" />
                        <xsl:for-each select="//section/*[$row]">
                            <td>
                                <xsl:value-of select="." />
                            </td>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </tbody>
        </table>
    </xsl:template>
</xsl:stylesheet>
Samuel
  • 2,106
  • 1
  • 17
  • 27
0
<table>
  <thead>
    <tr>
      <th></th>
      <th>AAA</th>
      <th>BBB</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Item1</td>
      <xsl:for-each select="Item1">
        <td><xsl:value-of select="."/></td>
      </xsl:for-each>
    </tr>
    <tr>
      <td>Item2</td>
      <xsl:for-each select="Item2">
        <td><xsl:value-of select="."/></td>
      </xsl:for-each>
    </tr>
    <tr>
      <td>Item3</td>
      <xsl:for-each select="Item3">
        <td><xsl:value-of select="."/></td>
      </xsl:for-each>
    </tr>
  </tbody>
</table>

If you need more variability, you will have to do some programming but that should start you.

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42