将incompleteXML数据XSLT转换为表

时间:2017-10-10 12:07:42

标签: html xml xslt xslt-2.0

我有以下XML文件

<root>
  <entries>
    <entry>
      <id>1</id>
      <title>entry A</title>
      <group>1</group>
      <category>a</category>
    </entry>
    <entry>
      <id>2</id>
      <title>entry B</title>
      <group>1</group>
      <category>c</category>
    </entry>
    <entry>
      <id>3</id>
      <title>entry C</title>
      <group>2</group>
      <category>b</category>
    </entry>
    <entry>
      <id>4</id>
      <title>entry D</title>
      <group>2</group>
      <category>c</category>
    </entry>
    <entry>
      <id>5</id>
      <title>entry E</title>
      <group>3</group>
      <category>a</category>
    </entry>
    <entry>
      <id>6</id>
      <title>entry F</title>
      <group>4</group>
      <category>c</category>
    </entry>
  </entries>
  <groups>
    <group id="1">
      <title>Group 1</title>
    </group>
    <group id="2">
      <title>Group 2</title>
    </group>
    <group id="3">
      <title>Group 3</title>
    </group>
    <group id="4">
      <title>Group 4</title>
    </group>
  </groups>
  <categories>
    <category id="a">
      <title>A</title>
    </category>
    <category id="b">
      <title>B</title>
    </category>
    <category id="c">
      <title>C</title>
    </category>
  </categories>
</root>

其中包含表定义(groups节点对应于行,category节点对应于列)和表条目。每个条目由组和列ID标识,并且条目不是针对所有单元格定义。

我需要一个表作为输出,如下所示:

<table>
  <thead>
    <tr>
      <th>Groups</th>
      <th>A</th>
      <!-- category name -->
      <th>B</th>
      <!-- category name -->
      <th>C</th>
      <!-- category name -->
    </tr>
  </thead>
  <tbody>
    <tr>
      <th group-id="1">Group 1</th>
      <!-- group name -->
      <th group-id="1" category-id="a"><span>entry A</span></th>
      <th group-id="1" category-id="b"></th>
      <th group-id="1" category-id="c"><span>entry B</span></th>
    </tr>
    <tr>
      <th group-id="3">Group 2</th>
      <!-- group name -->
      <th group-id="2" category-id="a"></th>
      <th group-id="2" category-id="b"><span>entry C</span></th>
      <th group-id="2" category-id="c"><span>entry D</span></th>
    </tr>
    <tr>
      <th group-id="3">Group 3</th>
      <!-- group name -->
      <th group-id="3" category-id="a"><span>entry E</span></th>
      <th group-id="3" category-id="b"></th>
      <th group-id="3" category-id="c"></th>
    </tr>
    <tr>
      <th group-id="4">Group 4</th>
      <!-- group name -->
      <th group-id="4" category-id="a"></th>
      <th group-id="4" category-id="b"></th>
      <th group-id="4" category-id="c"><span>entry F</span></th>
    </tr>
  </tbody>
</table>

如果初始xml中没有数据,则会用空跨度填充表格的所有单元格。

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

根据您的评论,您可能希望使用键来交叉引用您的指定组:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output method="html" indent="yes"/>

<xsl:key name="group-title" match="groups/group/title" use="../@id"/>

<xsl:key name="entry-group" match="entries/entry" use="group"/>

<xsl:template match="root">
    <table>
        <thead>
            <tr>
              <th>Groups</th>
              <xsl:for-each select="categories/category">
                  <th>
                      <xsl:value-of select="title"/>
                  </th>
              </xsl:for-each>
            </tr>
        </thead>
        <tbody>
            <xsl:variable name="categories" select="categories/category"/>
            <xsl:for-each select="groups/group">
                <tr>
                    <td group-id="{@id}">
                        <xsl:value-of select="title"/>
                    </td>
                    <xsl:variable name="group-id" select="@id"/>
                    <xsl:for-each select="$categories">
                        <xsl:variable name="group" select="key('entry-group', $group-id)"/>
                        <td group-id="{$group-id}" category-id="{@id}">
                            <xsl:value-of select="$group[category = current()/@id]/title"/>
                        </td>
                    </xsl:for-each>                 
                </tr>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>

</xsl:transform>

http://xsltransform.net/bEzjRKP/1