Html 处理动态创建的子级和同级';儿童
我一直在编辑和研究这个问题,但我没有找到一个看似简单的解决方案 一个元素(及其同级)将有任意数量的不同(动态)结果,我想在HTML表中显示这些结果,每个cd文件对显示一行。起初我认为我可以避免for each循环(不管怎样,它似乎返回相同的结果),我需要一种[更好的]方法来引用孩子们。下面的源代码在结构上与我正在使用的类似 例如,一些“cd”和“文件”将包含更多的元素,如价格,一些可能没有信息 XMLHtml 处理动态创建的子级和同级';儿童,html,xslt,xslt-2.0,Html,Xslt,Xslt 2.0,我一直在编辑和研究这个问题,但我没有找到一个看似简单的解决方案 一个元素(及其同级)将有任意数量的不同(动态)结果,我想在HTML表中显示这些结果,每个cd文件对显示一行。起初我认为我可以避免for each循环(不管怎样,它似乎返回相同的结果),我需要一种[更好的]方法来引用孩子们。下面的源代码在结构上与我正在使用的类似 例如,一些“cd”和“文件”将包含更多的元素,如价格,一些可能没有信息 XML 现在,我想我知道发生了什么。根据我的解释,在XSLT文件的“cd”和“文件”模板下,sel
现在,我想我知道发生了什么。根据我的解释,在XSLT文件的“cd”和“文件”模板下,select=“”
将返回
中的所有子级。我需要一种方法让每个人都有一个
。我尝试过为每个“cd”和“文件”以及“cd”和“文件”模板中的每个调用模板。请注意,我还需要动态构建
元素,因为现在它们是为测试目的手动创建的。有什么建议吗?作为一个有根据的猜测(不清楚您的预期输出是什么样子,输入文件的哪些方面是可变的),我认为您需要以下内容
无需显式地将模板应用于cd
和title
元素,只需以一般方式apply templates
。此外,您的样式表当前缺少存储
、大小
等的模板。据我所知,这些元素的内容应该放在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>
<!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>
我的CD收藏
HTML输出
<?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>
<!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>
我的CD收藏
标题
艺术家
商店
大小
皇帝讽刺剧
鲍勃·迪伦
苹果
3823
隐藏你的心
邦尼泰勒
亚马逊
2123
呈现的HTML
在线尝试此解决方案 作为一个有根据的猜测(不清楚您预期的输出是什么样子,输入文件的哪些方面是可变的),我认为您需要以下内容
无需显式地将模板应用于cd
和title
元素,只需以一般方式apply templates
。此外,您的样式表当前缺少存储
、大小
等的模板。据我所知,这些元素的内容应该放在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>
<!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>
我的CD收藏
HTML输出
<?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>
<!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>
我的CD收藏
标题
艺术家
商店
大小
皇帝讽刺剧
鲍勃·迪伦
苹果
3823
隐藏你的心
邦尼泰勒
亚马逊
2123
呈现的HTML
在线尝试此解决方案 如果我猜对了,您可以执行以下操作:
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>
我的CD收藏
这将为每个相册创建一行,并为每个带有文本节点的叶节点创建一列
请注意,这假设您的输入是常规的,即每个相册都有相同的属性,顺序相同,并且没有一个是空的。如果我猜对了,您希望执行以下操作:
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>
我的CD收藏
这将为每个相册创建一行,并为每个带有文本节点的叶节点创建一列
请注意,这假设您的输入是常规的,即每个相册都有相同的内容