我试图用XML和XSLT文件创建一个XHTML。
我只能合并第一列,第二列,但我无法合并第三列,应根据第一列合并第二列。
XML代码:
<?xml version="1.0" encoding="UTF-8"?>
<LIST>
<Row>
<TITLE>Empire Burlesque</TITLE>
<YEAR>2000</YEAR>
<artist>Bob Dylan</artist>
<artist1>Bob Dylan1</artist1>
</Row>
<Row>
<TITLE>Empire Burlesque</TITLE>
<YEAR>2000</YEAR>
<artist>Bob Dylan</artist>
<artist1>Bob Dylanas</artist1>
</Row>
<Row>
<TITLE>Empire Burlesque</TITLE>
<YEAR>2000</YEAR>
<artist>Bonnie Tyler</artist>
<artist1>Bob Dylan</artist1>
</Row>
<Row>
<TITLE>Empire Burlesque</TITLE>
<YEAR>2000</YEAR>
<artist>Bonnie Tyler</artist>
<artist1>Bob Dylanas</artist1>
</Row>
</LIST>
XSLT代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:key name="cds" match="Row" use="TITLE" />
<xsl:key name="cds2" match="Row" use="artist" />
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Year</th>
<th>Artist</th>
<th>Artist1</th>
</tr>
<xsl:for-each select="LIST/Row[generate-id() = generate-id(key('cds', TITLE)[1])]" >
<tr>
<td>
<xsl:if test="key('cds', TITLE)[1]">
<xsl:attribute name="rowspan">
<xsl:value-of select="count(key('cds', TITLE))" />
</xsl:attribute>
</xsl:if>
<xsl:value-of select="TITLE"/>
</td>
<td>
<xsl:if test="key('cds', TITLE)[1]">
<xsl:attribute name="rowspan">
<xsl:value-of select="count(key('cds', TITLE))" />
</xsl:attribute>
</xsl:if>
<xsl:value-of select="YEAR"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
<td>
<xsl:value-of select="artist1"/>
</td>
</tr>
<xsl:for-each select="key('cds', TITLE)[position() > 1]">
<tr>
<td>
<xsl:value-of select="artist"/>
</td>
<td>
<xsl:value-of select="artist1"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我需要这个表看起来像这样:
答案 0 :(得分:0)
您已定义第一个密钥,正确分组TITLE
,但对于第二个密钥,您在artist
内按TITLE
进行分组,因此您需要一个连锁密钥< / p>
<xsl:key name="cds2" match="Row" use="concat(TITLE,'|',artist)" />
请注意,管道字符|
可以是任何字符,只要它不会出现在TITLE
或artist
中。
获取不同的TITLE
元素是以当前的方式完成的:
<xsl:for-each select="LIST/Row[generate-id() = generate-id(key('cds', TITLE)[1])]">
要在其中获取不同的artist
元素,然后执行此操作
<xsl:for-each select="key('cds', TITLE)[generate-id() = generate-id(key('cds2', concat(TITLE,'|',artist))[1])]">
试试这个XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:key name="cds" match="Row" use="TITLE" />
<xsl:key name="cds2" match="Row" use="concat(TITLE,'|',artist)" />
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Year</th>
<th>Artist</th>
<th>Artist1</th>
</tr>
<xsl:for-each select="LIST/Row[generate-id() = generate-id(key('cds', TITLE)[1])]">
<xsl:for-each select="key('cds', TITLE)[generate-id() = generate-id(key('cds2', concat(TITLE,'|',artist))[1])]">
<xsl:variable name="artistPos" select="position()" />
<xsl:for-each select="key('cds2',concat(TITLE,'|',artist))">
<tr>
<xsl:if test="$artistPos = 1 and position() = 1">
<td rowspan="{count(key('cds', TITLE))}">
<xsl:value-of select="TITLE"/>
</td>
<td rowspan="{count(key('cds', TITLE))}">
<xsl:value-of select="YEAR"/>
</td>
</xsl:if>
<xsl:if test="position() = 1">
<td rowspan="{count(key('cds2',concat(TITLE,'|',artist)))}">
<xsl:value-of select="artist"/>
</td>
</xsl:if>
<td>
<xsl:value-of select="artist1"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>