在XSLT中创建列
请帮助使用基本XSLT模板为每个项目创建列。 输入XML:在XSLT中创建列,xslt,xslt-1.0,Xslt,Xslt 1.0,请帮助使用基本XSLT模板为每个项目创建列。 输入XML: <list> <item> <name>John</name> <image>John Picture</image> </item> <item> <name>Bob</name> <image>Bob Picture
<list>
<item>
<name>John</name>
<image>John Picture</image>
</item>
<item>
<name>Bob</name>
<image>Bob Picture</image>
</item>
</list>
约翰
约翰图片
上下快速移动
鲍勃图片
输出HTML:
<table>
<tr>
<td>John</td>
<td>Bob</td>
</tr>
<tr>
<td>John Picutre</td>
<td>Bob Picture</td>
</tr>
</table>
约翰
上下快速移动
约翰·皮克特
鲍勃图片
提前非常感谢您需要发布为获得结果而编写的XSLT,下面是您可以使用的代码: 最终更新脚本:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="list">
<table>
<tr>
<xsl:for-each select="item/name">
<td>
<xsl:value-of select="."/>
</td>
</xsl:for-each>
</tr>
<tr>
<xsl:for-each select="item/image">
<td>
<xsl:value-of select="."/>
</td>
</xsl:for-each>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
如果您想为每个
项目
元素创建一列,那么您应该从仅选择第一个项目
元素下的元素开始,因为这些元素将代表每行的开始
<xsl:for-each select="item[1]/*">
虽然这样定义一个键可能会更容易
<xsl:key name="items" match="item/*" use="name()" />
然后得到同名元素,如下所示:
<xsl:apply-templates select="key('items', name())" />
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:key name="items" match="item/*" use="name()" />
<xsl:template match="list">
<table>
<xsl:for-each select="item[1]/*">
<tr>
<xsl:apply-templates select="key('items', name())" />
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="item/*">
<td>
<xsl:value-of select="." />
</td>
</xsl:template>
</xsl:stylesheet>
这假设每个
项下都存在所有元素(至少在第一个项下存在)。您为此做了些什么吗?是的,我尝试了几种方法,但只是不想使线程混乱。对不起,我的错误:(我刚刚更新了所需的输出。表示生成了什么?这有助于实现所需的输出吗?抱歉,没有。我需要生成列(在开始时,我在输出中错误地生成了行)。几分钟前更新了所需的输出。我的意思是-每个人-单独的列(不是行)这里是新代码的新链接:抱歉。我想我错过了一些东西。这给我带来了一堆空的TRsI测试结果,所以您使用的XML可能与您的问题中显示的略有不同?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:key name="items" match="item/*" use="name()" />
<xsl:template match="list">
<table>
<xsl:for-each select="item[1]/*">
<tr>
<xsl:apply-templates select="key('items', name())" />
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="item/*">
<td>
<xsl:value-of select="." />
</td>
</xsl:template>
</xsl:stylesheet>