-1

假设我有这个 XML 文件

<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>

现在我想创建一个 xsl 文件,它将在表格中显示数据,如下所示

           AAA          BBB
Item1      FALSE        FALSE
Item2      FALSE        FALSE
Item3      FALSE        FALSE

我尝试了几种语法,但都没有给我我想要的我可以在这里获得帮助或示例吗?

4

2 回答 2

1

以下解决方案适用于任意数量的部分和任意数量的项目,前提是每个部分具有相同数量的项目:

<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>
于 2013-11-27T15:52:13.083 回答
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>

如果你需要更多的可变性,你将不得不做一些编程,但这应该让你开始。

于 2013-11-27T14:43:35.957 回答