XSLT转换成表
问题描述:
我已经下面的XML文件XSLT转换成表
<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>
包含表定义(基团节点对应于行和类别节点对应于列)和表条目。每个条目由组和列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没有数据。
任何帮助表示赞赏!
答
根据您的意见,你可能需要使用钥匙交叉引用从您给出组:
<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>
+0
谢谢!这对我有效! – oachkatzlschwoaf
嗯,你行你想如' ... xsl:for-each-group>'和每个'tr'的内容填充从中从已知的 的选择=“current-group()/ category [。= current()]”/> xsl:for-each>'或使用类似的apply-templates。 –
嗨!谢谢您的回复! ... xsl:for-each-group>可以正常工作,如果我能涵盖所有组通过条目。可以有一些空的组,没有任何条目。 –
oachkatzlschwoaf
我只是试图在http://xsltransform.net/bEzjRKP上一起提琴,至少对于发布的示例来说,它看起来似乎有效。但是你当然也可以处理'groups/group'在表体中创建'tr'行并使用一个键来访问相应的'entry'元素。 –