我一直在编辑和研究这个问题,很久以来,我一直未能找到一个看似简单的解决方案。
元素(及其同级)将具有任意数量的不同(动态)结果,我希望在HTML表中显示这些结果,每个cd文件对都显示一行。一开始我认为我可以避免-每个循环(这似乎是返回相同的结果无论如何),我需要一个更好的方法来参考孩子。下面的源代码在结构上与我所使用的相似。
例如,一些“cd”和“file”将包含更多的元素,如价格,有些可能没有信息。
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.test2.xsl"?>
<catalog>
<rock>
<album>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
</cd>
<file>
<store>Apple</store>
<size>3823</size>
</file>
</album>
<album>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
</cd>
<file>
<store>Amazon</store>
<size>2123</size>
</file>
</album>
</rock>
</catalog>XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#cccccc">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
<th style="text-align:left">Store</th>
<th style="text-align:left">Size</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="album">
<tr>
<xsl:apply-templates select="cd"/>
<xsl:apply-templates select="file"/>
</tr>
</xsl:template>
<xsl:template match="cd">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
<xsl:template match="file">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>结果
<table border="1">
<tbody>
<tr bgcolor="#cccccc">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
<th style="text-align:left">Store</th>
<th style="text-align:left">Size</th>
</tr>
<tr>
<td>
Empire Burlesque
Bob Dylan
</td>
<td>
Apple
3823
</td>
</tr>
<tr>
<td>
Hide your heart
Bonnie Tyler
</td>
<td>
Amazon
2123
</td>
</tr>
</tbody>
</table>现在,我想我知道发生了什么。根据我的解释,在XSLT文件中,在"cd“和" file”模板下,select="."将返回<td>中的所有子元素。我需要一种方法让每个人都有一个<td>。我已经尝试过这样的东西,比如每个"cd“和"file”的调用模板,以及"cd“和"file”模板中的每个调用模板。请注意,我还需要动态构建<th>元素,因为现在它们是为测试目的手动创建的。有什么建议吗?
发布于 2017-02-10 16:22:46
作为一个合理的猜测(不太清楚您的预期输出是什么样子,以及输入文件的哪些方面是可变的),我认为您需要以下内容。
不需要将模板显式地应用于cd和title元素,只需要以一般的方式应用apply-templates。另外,您的样式表目前缺少store、size等模板。据我所知,这些元素的内容应该放在HTML中的td元素中,因此您需要将它们与模板匹配,即使这些模板是相当通用的。
如果您事先不知道表头th的名称,如果album的孙辈叶元素的数量是可变的,那么这里有一个更“动态”的解决方案:
XSLT样式表
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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:strip-space elements="*"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#cccccc">
<xsl:for-each select="distinct-values(catalog/rock/album/*/*/name())">
<th style="text-align:left">
<xsl:value-of select="."/>
</th>
</xsl:for-each>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="album">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="album/*/*">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>HTML输出
<!DOCTYPE html
PUBLIC "XSLT-compat">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#cccccc">
<th style="text-align:left">title</th>
<th style="text-align:left">artist</th>
<th style="text-align:left">store</th>
<th style="text-align:left">size</th>
</tr>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
<td>Apple</td>
<td>3823</td>
</tr>
<tr>
<td>Hide your heart</td>
<td>Bonnie Tyler</td>
<td>Amazon</td>
<td>2123</td>
</tr>
</table>
</body>
</html>呈现的

尝试此解决方案在线这里。
发布于 2017-02-10 16:37:19
如果我猜对了,你想做的事情是:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="/catalog">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr>
<xsl:apply-templates select="*/album[1]" mode="header"/>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="album">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="*[text()]" mode="header">
<th>
<xsl:value-of select="name()"/>
</th>
</xsl:template>
<xsl:template match="*[text()]">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>这将为每个album创建一行,为每个带文本节点的叶节点创建列。
请注意,这假设您的输入是正常的-即每个专辑都有相同的属性,按相同的顺序,并且没有一个是空的。
https://stackoverflow.com/questions/42163822
复制相似问题